 if($.cookie("redtricks")) {
     $("link").attr("href",$.cookie("redtricks"));
}
$(document).ready(function () {
	var anim_id;
    //var siteName = 'http://localhost/redtricks/';
	var siteName = 'http://www.redtricks.com/';
	var href = window.location.href;
    href = href.replace(siteName, "");
	// check to see if have # in string
	if(href.indexOf('#') != -1) {
		var href_hash_array = href.split('#');
		href = href_hash_array[0];
	}
	if(href.length == 0) {
		href = "index.php";
	}
   $('.top_nav li a').each(function () {
        if ($(this).attr('href') == href) {
            $(this).parent().addClass('active');
        } else {
            $(this).parent().removeClass('active');
        }
    });
   // style switcher
   $(".style_switcher ul li a").click(function() { 
		$("link").attr("href",$(this).attr('rel'));
		$.cookie("redtricks",$(this).attr('rel'), {expires: 365, path: '/'});
		swap_images();
		roll_over();
		return false;
	});
	// tooltips
	var tt_down = 32;
	var tt_left = -10;
	$('.top_nav ul li a').tooltip({ effect: 'slide', offset:[tt_down,tt_left], position: 'top center'});
	// click tooltips
	$('.top_nav ul li').click(function() {
		var myUrl = $(this).find('a').attr('href');
		// check for contact
		if(myUrl == "#contact") {
			window.location = myUrl;
			$('#firstname').focus();
		} else {
			window.location = myUrl;
		}
	});	
	function swap_images()
	{
		var stylesheet_array = $('link').attr('href').split('/');
		var stylesheet_file = stylesheet_array[stylesheet_array.length - 1];
		switch(stylesheet_file) {
			case 'style.css':
				$('.rollover').each(function() {
					//alert("img_file")
					var img_file_array = $(this).attr('src').split('/');
					var img_file = img_file_array[img_file_array.length - 1];
					//alert(img_file)
					$(this).attr('src', 'images/' + img_file);
				});
			 break;
			 case 'style2.css':
				$('.rollover').each(function() {
					//alert("img_file")
					var img_file_array = $(this).attr('src').split('/');
					var img_file = img_file_array[img_file_array.length - 1];
					//alert(img_file)
					$(this).attr('src', 'images2/' + img_file);
				});
			 break;
		}
	}
	function roll_over() 
	{
		$('.rollover').each(function() {
			var imgFile = $(this).attr('src');
			var preloadImage = new Image;
			var imgExt = /(\.\w{3,4}$)/;
			preloadImage.src = imgFile.replace(imgExt, '_over$1');
			$(this).hover(
				function() {
					$(this).attr('src', preloadImage.src);
				},
				function() {
					$(this).attr('src', imgFile);
				}
			); 
		});
	}
	// show hide websites menu
	$('#sidebar_title_link').click(function() {
		do_sidebar_title();
	});
	do_sidebar_title = function() {
		$('.paging').toggle('slow', function() {
			$('#an_play').show();
			$("#an_pause").hide();
			// set the bg image of the title
			if($('.paging').is(':visible')) {
				$('#sidebar_title').removeClass('sidebar_up');
				$('#sidebar_title').addClass('sidebar_down');
				var listItem = $('.active');
				var index =  $('.paging li').index(listItem);
				rotate();
			} else {
				$('#sidebar_title').removeClass('sidebar_down');
				$('#sidebar_title').addClass('sidebar_up');
				// stop the animation
				$(".anim_container").stop(true,true);
				clearInterval(anim_id);
			}
		});
	}
	// initialise anims
	// anim controls
	$('#an_next a').click(function(event) {
		event.preventDefault();
		var wrapper =  $('.anim_container .wrapper');
		var anim_con =  $('.anim_container');
		clearInterval(anim_id);
		$(anim_con).stop(true,true);
		$("#an_play").hide();
		$("#an_pause").show();
		var position = $(anim_con).position();
		var index = Math.round((position.left * -1)/$(wrapper).width());
		//alert( $(wrapper).length - 1);
		if(index != $(wrapper).length - 1) {
			index = index + 1;
			move_wrapper(anim_con,wrapper,index);
			add_paging_highlight(index);
		}
	});
	$('#an_back a').click(function(event) {
		event.preventDefault();
		var wrapper =  $('.anim_container .wrapper');
		var anim_con =  $('.anim_container');
		clearInterval(anim_id);
		$(anim_con).stop(true,true);
		$("#an_play").hide();
		$("#an_pause").show();
		var position = $(anim_con).position();
		var index = Math.round((position.left * -1)/$(wrapper).width());
		if(index != 0) {
			index = index -1;
			move_wrapper(anim_con,wrapper,index);
			add_paging_highlight(index);
		}
	});
	$("#an_play a").click(function(event) {
		event.preventDefault();
		if($('.paging').is(':visible') == false) {
			$('#sidebar_title').removeClass('sidebar_up');
			$('#sidebar_title').addClass('sidebar_down');
			$('.paging').show('slow')
		}
		var listItem = $('.active');
		var index =  $('.paging li').index(listItem);
		// show and hide play pause buttons
		$("#an_play").hide();
		$("#an_pause").show();
		rotate();
   });
	$("#an_pause a").click(function(event) {
		event.preventDefault();
	  // show and hide play pause buttons
	  $("#an_pause").hide();
	  $("#an_play").show();
	  clearInterval(anim_id);
	  $(".anim_container").stop(true,true);
    });
	function add_paging_highlight(num)
	{
		$('.paging li').each(function() {
			$('.paging li').removeClass('active');
		});
		$('.paging li:eq('+num+')').addClass('active');
		//$('.paging li:eq(3)').addClass('active');
	}
	$('.paging li').click(function(event) {
		event.preventDefault();
		$(".anim_container").stop(true,true);
		clearInterval(anim_id);
		$("#an_play").hide();
		$("#an_pause").show();
		var index = $('.paging li').index(this);
		add_paging_highlight(index);
		var wrapper =  $('.anim_container .wrapper');
		var anim_con =  $('.anim_container');
		move_wrapper(anim_con,wrapper,index);
	});
	function move_wrapper(anim_con,wrapper,index)
	{
		//alert('parent ' + $(anim_con).parent().parent().attr('id'));
		var num_items = $(wrapper).length;
		var wrapper_width = $(wrapper).width();
		var max_move = ((wrapper_width * num_items) - wrapper_width)
		
		$(anim_con).css({'width' : num_items * wrapper_width});
		
		var position = $(anim_con).position();
		var move_to = -(wrapper_width * index)
			$(anim_con).animate({
				left: move_to,
			  }, 400, function() {
				// Animation complete.
					rotate();
			 });
	}
	// popup
	$('.ta .desc').click(function() {
		var img = $(this).parent().find('img');
		var im_src = $(img).attr('src');
		im_src = im_src.replace("thumbs", "big_images");
		var pop_w = 570;
		var pop_h = 380;
		$('.popup img').attr('src', im_src);
		$('.pop_title').text($(this).parent().find('.caption').text());
		$('.popup').fadeIn();
		
		var pop_top = (pop_h + 80) / 2;
		var pop_left = (pop_w + 80) / 2;
	
		//do margin
		$('.popup').css({
			'margin-top' : -pop_top,
			'margin-left' : -pop_left
		});
	
		//Fade in Background
		$('body').append('<div id="fade"></div>'); 
		$('#fade').css({'filter' : 'alpha(opacity=60)'}).fadeIn(); 
	
		return false;
	});
	
	//Close Popups and Fade Layer
	$('.pop_btn_close, #fade').live('click', function() { 
		$('#fade , .popup').fadeOut(function() {
			$('#fade').remove(); 
		});
		return false;
	});
	function rotate()
	{
		anim_id = setInterval('anim()', 5000);
	}
	
	anim = function()
	{
		var listItem = $('.paging .active');
		//alert('listItem : ' + $('.paging li').index(listItem));
		var index = $('.paging li').index(listItem) + 1;

		//alert('index ' + index);
		var anim_con = $(' .anim_container');
		var num_items = $('.anim_container .wrapper').length;
		var wrapper_width = $('.anim_container .wrapper').width();
		var max_move = ((wrapper_width * num_items) - wrapper_width)
		
		$(anim_con).css({'width' : num_items * wrapper_width});
		
		var position = $(anim_con).position();
		var move_to = -(wrapper_width * index)
		//alert('move to ' + move_to)
		if(index < num_items) {
			$(anim_con).animate({
				left: move_to,
			  }, 400, function() {
				// Animation complete.
				$('.paging li.active').next().addClass("active");
				$(listItem).removeClass('active');
			 });
		} else {
			$(anim_con).animate({
				left: '0',
			  }, 400, function() {
				// Animation complete.
				$(listItem).removeClass('active');
				$('.paging li').first().addClass("active");
			 });
		}	
	}
	$('.rollover').each(function() {
		var imgFile = $(this).attr('src');
		var preloadImage = new Image;
		var imgExt = /(\.\w{3,4}$)/;
		preloadImage.src = imgFile.replace(imgExt, '_over$1');
		$(this).hover(
			function() {
				$(this).attr('src', preloadImage.src);
			},
			function() {
				$(this).attr('src', imgFile);
			}
		); 
	});
	rotate();
	$('#an_play').hide();
	$("#an_pause").show();
}); // end ready
