/* IE6 flicker fix
-------------------------------------------------- */
try { document.execCommand("BackgroundImageCache", false, true); } catch(err){}
$.browser.msie6 = $.browser.msie && /MSIE 6\.0/i.test(window.navigator.userAgent) && !/MSIE 7\.0/i.test(window.navigator.userAgent);

/* Init */

$(document).ready(function() {
	$('.ihasahover').hover(function(){ $(this).addClass('hover'); },function(){ $(this).removeClass('hover'); });
	$("#TB_closeWindowButton").click(function(){ 
	window.parent.tb_remove();
	});
	$("UL.type02 LI").mouseenter(function(){
		$(this).addClass("hover");
		if(!$.browser.msie){
			grayscale.reset($("img",this));
		}
		else{
			$("img",this).each(function(){this.style.filter = 'progid:DXImageTransform.Microsoft.BasicImage(grayscale=0)'});
		}
	})
	.mouseleave(function(){
		$(this).removeClass("hover")
		if(!$.browser.msie){
			grayscale($(this).find("img"));
		}
		else{
			$("img",this).each(function(){this.style.filter = 'progid:DXImageTransform.Microsoft.BasicImage(grayscale=1)'});
		}
	})
	if($('#contentslider').length>0){
		$('#contentslider').slideshow();
	}
	if($(".portfolio").length>0){
		initPortfolio();
	}
	if(!$.browser.msie){
		
		$('.portfolio img').load(function(){
			grayscale($(this));
			$(this).unbind('load');
		})
	}
	else{
		$('.portfolio img').each(function(){
			this.style.filter = 'progid:DXImageTransform.Microsoft.BasicImage(grayscale=1)';
		});
	}
	$("UL.type01").each(function(){
		var children = $(this).children("LI");
		var len = children.length;
		var i = 0;
		while(i<len){
			var maxHeight=0;
			for(var j = i; j<i+3; j++){
				var currHeight = children.eq(j).height();
				if(currHeight>maxHeight) maxHeight=currHeight;				
			}
			for(var j = i; j<i+3; j++){
				 children.eq(j).css("height",maxHeight+"px");
			}
			i+=3;
		}
	})
});


/* Portfolio */
function initPortfolio(){
	$(".portfolio LI").click(function(e){
		if(!$(e.target).hasClass("visit")){
			var targ = $(this);
			var c = $("P.fulltext",targ).text() || $("P.description",targ).text();
			var a = $("UL.images A:first",targ).attr("href") || $("UL.images A:first",targ).attr("alt") || $(".more",targ).attr("href");
			var g = $("UL.images A:first",targ).attr("rel") || false;
			var l = $(".visit",targ).attr("href") || false;
			var s = $(".visit",targ).text() || false;
			var t = $("h5",targ).text() || false;
			var u = $("P.subtitle",targ).text() || false;
			tb_show(c,a,g,l,s,t,u);
			this.blur();
			return false;
		}
	})
	
	$(".portfolio").each(function(){
		var children = $(this).children("LI");
		var len = children.length;
		var i = 0;
		while(i<len){
			var maxHeight=0;
			for(var j = i; j<i+3; j++){
				var currHeight = children.eq(j).height();
				if(currHeight>maxHeight) maxHeight=currHeight;				
			}
			for(var j = i; j<i+3; j++){
				 children.eq(j).css("height",maxHeight+"px");
			}
			i+=3;
		}
	})
}

/* Tabs switcher */

function showTab(ref,set){
	if( $(ref).hasClass('active') == true ){ return false; }
	$('#'+set+' > LI > A').each(function(i,a){
		$(a).removeClass('active');
		//$($(a).attr('href')).addClass('hidden');
		if( $($(a).attr('href')).hasClass('hidden') == false ){
			$($(a).attr('href')).slideUp(300,function(){
				$(this).addClass('hidden');
			});
		}
	});
	$(ref).addClass('active');
	$($(ref).attr('href')).slideDown(300).removeClass('hidden').css({display: ''});
	return false;
}
var rotateInterval;
var mouseWithin=false;
   
(function($) {
  $.fn.slideshow = function(customOptions) {
	 var options = {
      action: "init",
	  pos: -1
    };
    $.extend(options, customOptions);
    $targ = $(this);
	
	if(options.action=="init"){
		$('.images li',$targ).css({opacity: 0.0});
		$('.imagedata li',$targ).css({display:'none'});
		
		var imagecount = $('.images li',$targ).length;
		$('ul.bullets',$targ).empty();
		var i = 1;
		$('.images li',$targ).each(function(){
			$(this).css("z-index",i*10);
			$('ul.bullets',$targ).append('<li><a href="#"></a></li>');
			i++;
		});
		//Get the first image and display it (gets set to full opacity)
		$('.images li:first',$targ).css({opacity: 1.0});
		$('.imagedata li:first',$targ).css({display:'block'})
			.addClass("show");
		
		$('ul.bullets li:first',$targ).addClass("active");
		if(imagecount>1){
			$targ.mouseover(function(){
				clearInterval(rotateInterval);
				mouseWithin = true;
			})
			.mouseout(function(){
				clearInterval(rotateInterval);
				//rotateInterval = setInterval('$("#'+$targ.attr("id")+'").slideshow({action:"rotate",pos:-1})',100);
				mouseWithin = false;
			})
			$('ul.bullets li a',$targ).click(function(){
				$('ul.bullets li',$targ).removeClass("active");
				$(this).parent().addClass("active");
				var newpos = $('ul.bullets li',$targ).index($(this).parent());	
				clearInterval(rotateInterval);
				$("#"+$targ.attr("id")).slideshow({action:"rotate",pos:newpos});	
				//rotateInterval = setInterval('$("#'+$targ.attr("id")+'").slideshow({action:"rotate",pos:-1})',6000);
				$targ.mouseover();
				return false;
			});
			$('#contentslider .btnLeft').click(function(){
				clearInterval(rotateInterval);
				var current = ($('.images li.show',$targ)?  $('.images li.show',$targ) : $('.images li:first',$targ));
				if ( current.length == 0 ) current = $('.images li:first',$targ);
				var newpos = $('.images li',$targ).index(current);
				if(newpos>0){
					$targ.slideshow({action:"rotate",pos:newpos-1});					
				}
				else{
					$targ.slideshow({action:"rotate",pos:$('.images li',$targ).length-1});
				}
				$targ.mouseover()
				return false;
			})
			$('.btnRight',$targ).click(function(){
				clearInterval(rotateInterval);
				var current = ($('.images li.show',$targ)?  $('.images li.show',$targ) : $('.images li:first',$targ));
				if ( current.length == 0 ) current = $('.images li:first',$targ);
				var newpos = $('.images li',$targ).index(current);
				if(newpos<$('.images li',$targ).length-1){
					$targ.slideshow({action:"rotate",pos:newpos+1});
				}
				else{
					$targ.slideshow({action:"rotate",pos:0});
				}
				$targ.mouseover()
				return false;
			})
			//Call the rotator function to run the slideshow, 6000 = change to next image after 6 seconds
			
			//rotateInterval = setInterval('$("#'+$targ.attr("id")+'").slideshow({action:"rotate",pos:-1})',6000);
		}
	}
	else if(options.action=="rotate"){		
		clearInterval(rotateInterval);			
		//Get the first image
		var current = ($('.images li.show',$targ)?  $('.images li.show',$targ) : $('.images li:first',$targ));
		var currenttext = ($('.imagedata li.show',$targ)?  $('.imagedata li.show',$targ) : $('.imagedata li:first',$targ));
		var currentdot = ($('ul.bullets li.active',$targ)?  $('ul.bullets li.active',$targ) : $('ul.bullets li:first',$targ));
		
		if ( current.length == 0 ) current = $('.images li:first',$targ);
		if ( currentdot.length == 0 ) currentdot = $('ul.bullets li:first',$targ);
		var pos = options.pos;
		//Get next image, when it reaches the end, rotate it back to the first image
		if(pos>=0){
			var next = $('.images li',$targ).eq(pos);
			var nextdot = $('ul.bullets li',$targ).eq(options.pos);
		}
		else{
			var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('.images li:first',$targ) :current.next()) : $('.images li:first',$targ));
			var nextdot = ((currentdot.next().length) ? ((currentdot.next().hasClass('active')) ? $('ul.bullets li:first',$targ) :currentdot.next()) : $('ul.bullets li:first',$targ));
		}
		if ( currenttext.length != 0 ) {
			if(pos>=0){
				var nexttext = $('.imagedata li',$targ).eq(pos);
			}
			else{
				var nexttext = ((currenttext.next().length) ? ((currenttext.next().hasClass('show')) ? $('.imagedata li:first',$targ) :currenttext.next()) : $('.imagedata li:first',$targ));
			}
			nexttext.addClass('show')
				.show();
		
			//Hide the current image
			currenttext.hide()
				.removeClass('show');
		}
		
		//Set the fade in effect for the next image, the show class has higher z-index
		next.css({opacity: 0.0})
			.addClass('show')
			.animate({opacity: 1.0}, 1000);
	
		//Hide the current image
		current.animate({opacity: 0.0}, 1000)
			.removeClass('show');
		
		currentdot.removeClass("active");
		nextdot.addClass("active");
		//if(mouseWithin==false){rotateInterval = setInterval('$("#'+$targ.attr("id")+'").slideshow({action:"rotate",pos:-1})',6000)};
	};    
  };
})(jQuery);




