// Background Postion Fix

$(document).ready(resizeWindow);
$(window).bind("resize", resizeWindow);
function resizeWindow() {
	var body_width = $('body').width()
	if (body_width <= 1000){
	//	$("div#total_entire_whole_site_container, div#whole_site_container, div#footer_container").css({'background-position':'500px 0px', 'width' : '1000px' });
	}
	else {
	//	$("div#total_entire_whole_site_container, div#whole_site_container, div#footer_container").css({'background-position':'center top', 'width' : '100%'});
	}
	
var twitter_left =  $("body").width()/2-500; 
$("#twitter").css("left", twitter_left);

}	 
	
	
// NORMAL RULES
$(document).ready(function() {

	if(typeof console === "undefined") {
		console = { log: function() { } };
	}


	// LAYOUT ESSENTIALS
	$("#navigation li a").hover( 
		function (){
			$(this).stop(true, false).animate({backgroundPosition: '0px 0px'},1200);			
		},
		function (){
			$(this).stop(true, false).animate({backgroundPosition: '-550px 0px'},1200);			
		} 
	); 
	
	$("body").append('<div id="footer_aid">&nbsp;</div>');
	
	
	// IE CHECK 
	if(typeof document.body.style.maxHeight === "undefined") {	
		var is_ie6 = true;
	} 
	else {
		var is_ie6 = false;
	}

	if (jQuery.browser.msie) {
		ie_v = parseInt(jQuery.browser.version);
	  	if (ie_v == 7){
			$("body").addClass("ie7");
	  	}
	}
	
	
	// SLIDER

	// USER DEFINED VARIABLES 
	
	slide_height = -820;
	slides_shown = 1;
	slide_selector = '#slides li';
	slide_selector_container = '#slides';
	
	// BROWSER DEFINED VARIABLES
	
	slide_current = 1;
	pos_slide_height = slide_height * -1;
	slide_amount =  $(slide_selector).length;
	$(slide_selector).addClass("jq_slides");
	slide_container_height = slide_height * slide_amount;
	new_slide_container_height = slide_height * -1 * slide_amount;
	single_group_length = slide_height * -1 * slides_shown;
	new_slide_container_height = new_slide_container_height + single_group_length;
	$(slide_selector_container).css({'height':new_slide_container_height});
	slide_duplicate_selector = slide_selector+':lt('+slides_shown+')'
	$(slide_duplicate_selector).clone().appendTo($(slide_selector_container));

    // SLIDER FUNCTIONS
	
	function slider(slide_current_overide) {
		if(slide_current_overide != "test"){
			if (slide_current_overide == "next") {
				slide_current = slide_current + 1;
			}
			else if (slide_current_overide == "prev") {
				slide_current = slide_current - 1;
			}
			else{
				slide_current = slide_current_overide;
			}
	
			if(slide_current == 0){
				$(slide_selector_container).animate({"top" : slide_container_height}, 0);
				slide_current = slide_amount;
			}
			
			slide_current_multiplier = slide_current - 1;
			top_slide_value = slide_current_multiplier * slide_height;	
			
			if (slide_current_overide == "next") {
				top_slide_value = '-='+pos_slide_height;
			}
			if (slide_current_overide == "prev") {
				top_slide_value = '+='+pos_slide_height;
			}
			else {
				$(slide_selector_container).animate({top : top_slide_value},{duration:0 });			
			}

			if (slide_current == slide_amount+1) {
				$(slide_selector_container).animate({"top" : "0"},0);
				slide_current = 1;
			}
		}
		else{
			if(slide_current == slide_amount+1){slide_current = 1};


			slide_current = slide_current + 1;
			
			slide_current_multiplier = slide_current - 1;
	
			top_slide_value = slide_current_multiplier * slide_height;
	
			$(slide_selector_container).animate({top : top_slide_value},{duration:900 });

			if (slide_current == slide_amount+1) {
				$(slide_selector_container).animate({"top" : "0"},0);
				slide_current = 1;
			}
		}	
	}


	// SLIDER INTRAVAL 
	
	rotating = setInterval(function() {
		slider("test");
	},8000)

	// HOVER PAUSE 
	
	$("#slides li div").hover(
		function (){			
			clearInterval(rotating);
		}, function(){
			rotating = setInterval(function() {
				slider("test");
			},8000)			
	});


	// SLIDER BUTTONS
/*
	$(".button_1").click( function(){
		clearInterval(rotating);
		slider(1);
	});
*/		


	subnav_left_bg_pos = 5;
	$("#sub_navigation ul li a").each(function () {
		subnav_left_bg_pos = subnav_left_bg_pos - 5;
		background_pos_statement = subnav_left_bg_pos+"px -36px"
		$(this).attr('rel', background_pos_statement);		
	});
	
	$("#sub_navigation ul li a").hover(
		function () {
			background_pos_statement = $(this).attr('rel');
			$(this).css({'background-position': background_pos_statement, 'color' : '#1f9ada'});
		}, function () {
			$(this).css({'background-position': '0px 0px', 'color' : '#898989'});
		}
	);
	
	
	/* PORTFOLIO ITEMS */

	
	function portfolio_page (portfolio_type) {

		base_ajax_url = "ajax_portfolio.php?portfolio_type=";
		ajax_url = base_ajax_url + portfolio_type; 
		
		$("#portfolio_nav li a").removeClass("portfolio_current");

		if (portfolio_type == "websites"){
			$("#portfolio_nav_websites").addClass("portfolio_current");
		}
		if (portfolio_type == "logos"){
			$("#portfolio_nav_logos").addClass("portfolio_current");
		}
		if (portfolio_type == "brands"){
			$("#portfolio_nav_brands").addClass("portfolio_current");
		}
		
		$('#portfolio_content').load(ajax_url, function (){
			Cufon.replace('#portfolio_items li p strong',{
				fontFamily: 'Spyroclassic'
			});
			
			
			$("a.portfolio_box, a.portfolio_logos, a.portfolio_brands").fancybox({ 
				'type':  		'image',								
				'autoScale':  		false,								
				'speedIn': 		100,
				'speedOut': 	100,
				'padding':      0, 
				'margin':      0, 
				'transitionIn': 'elastic',
				'transitionOut': 'elastic',
				'easingIn': 	'easeInSine',
				'easingOut': 	'easeOutSine',
				'titleShow' : false
			});
					
			$('#portfolio_items li').click( function (){
				if (portfolio_type == "websites"){
				$(this).find('a.portfolio_box').trigger('click');
				}
				if (portfolio_type == "logos"){
				$(this).find('a.portfolio_logos').trigger('click');
				}
				if (portfolio_type == "brands"){
				$(this).find('a.portfolio_brands').trigger('click');
				}
				
			});
	
			if (is_ie6 == false){
	
				$('#portfolio_items li, #portfolio_title').hide();
				$('#portfolio_title').show('slide', 800);
			
			}
	
			if (jQuery.browser.msie) {
				if (is_ie6 == false){
					$('#portfolio_items li').show("drop", 2000);
				}
			}
			else{
				$("#portfolio_items li").each(function(i) {
					setTimeout(function(list_item) {
						$(list_item).fadeIn();
					},i*150,this);
				});
			}
			
			if (is_ie6 == false){
	
				$('#portfolio_items li').each(function(){
					if (portfolio_type == "websites"){
						$(this).append('<a href="#"><img class="porfolio_item_mask" src="/images/misc_elements/portfolio_item_mask.png"/></a>');
					}
					else if (portfolio_type == "logos"){
						$(this).append('<img class="porfolio_item_mask" src="/images/misc_elements/portfolio_logo_mask.png"/>');
					}
					else {
						$(this).append('<img class="porfolio_item_mask" src="/images/misc_elements/portfolio_branding_mask.png"/>');
					}			
				});
		
				if (portfolio_type == "websites" || portfolio_type == "logos" ){
					$("#portfolio_items li").hover( function (){						  
							$(this).find(".porfolio_item_image").stop().animate({
								marginTop: '5px', 
								marginLeft: '5px', 
								width: '226px', 
								height: '132px'
								}, 230
							);
							$(this).find(".porfolio_item_mask").stop().animate({
								marginTop: '-10px', 
								marginLeft: '-10px', 
								width: '255px', 
								height: '206px'
								}, 300
							);
						}, function (){
							$(this).find(".porfolio_item_image").stop().animate({
								marginTop: '15px', 
								marginLeft: '15px', 
								width: '206px', 
								height: '120px'
								}, 300
							);
							$(this).find(".porfolio_item_mask").stop().animate({
								marginTop: '0px', 
								marginLeft: '0px', 
								width: '235px', 
								height: '190px'
								}, 300
							);
						}
					);
				}
				if (portfolio_type == "brands"){
					$("#portfolio_items li").hover( function (){						  
							$(this).find(".porfolio_item_image").stop().animate({
								marginTop: '5px', 
								marginLeft: '5px', 
								width: '226px', 
								height: '209px'
								}, 230
							);
							$(this).find(".porfolio_item_mask").stop().animate({
								marginTop: '-10px', 
								marginLeft: '-10px', 
								width: '255px', 
								height: '239px'
								}, 300
							);
						}, function (){
							$(this).find(".porfolio_item_image").stop().animate({
								marginTop: '15px', 
								marginLeft: '15px', 
								width: '206px', 
								height: '191px'
								}, 300
							);
							$(this).find(".porfolio_item_mask").stop().animate({
								marginTop: '0px', 
								marginLeft: '0px', 
								width: '235px', 
								height: '220px'
								}, 300
							);
						}
					);
				}
			}
		});
	} 
	
	
	function portfolio_selection_click (type) {
		$('#portfolio_title').hide('slide', 800);
		$('#portfolio_items li').hide("drop", 500, function () {
			if (callbacked == true){
				$('#portfolio_content').empty();		
				portfolio_page(type);
				callbacked = false;
			}
		});
	} 
	
	if($("#container_2").hasClass("portfolio")){
		
		if($("#portfolio_nav").hasClass("logos")){
			portfolio_page("logos");		
		}
		else if($("#portfolio_nav").hasClass("brands")){
			portfolio_page("brands");		
		}
		else{
			portfolio_page("websites");		
			$("#portfolio_nav_websites").addClass("portfolio_current");
		}
			

		$("#portfolio_nav_websites").click(function () {
			callbacked = true;													 
			portfolio_selection_click("websites");													 
			return false;			
		});
		
		$("#portfolio_nav_logos").click(function () {
			callbacked = true;											  
			portfolio_selection_click("logos");													 
			return false;			
		});
		
		$("#portfolio_nav_brands").click(function () {
			callbacked = true;											   
			portfolio_selection_click("brands");													 
			return false;			
		});

	}

	if($("#container_2").hasClass("showcase")){	
		$(".learn_more_link").toggle(									 
			function () {
				if (is_ie6 == false) {
					body_topic_img = $(this).parent("div").find("img").detach();
					if (body_topic_img == null) {
					}
					else {
						console.log(body_topic_img);
						$(this).parent("div").parent("div").append(body_topic_img);
					}
				}
				body_topic_height = $(this).parent("div").height();
				$(this).attr({name: body_topic_height});
				$(this).parent("div").css({'overflow':'hidden', 'height' : body_topic_height });
				$(this).parent("div").wrapInner('<div class="body_topic_height" />');
				$(this).parent("div").find(".showcase_elipse").css({'display':'none'});
				$(this).parent("div").find(".showcase_ctd_text").css({'display':'inline'});
				$(this).empty().append("&#171; close");
				body_topic_container_height = $(this).parent("div").height();
				console.log(body_topic_container_height);
				$(this).parent("div").parent("div").animate({'height': body_topic_container_height}, 600);
			return false;
			}, 
			function () {
				var body_topic_height = $(this).attr("name");
				$(this).parent("div").find(".showcase_ctd_text").css({'display': 'none'});
				$(this).parent("div").find(".showcase_elipse").css({'display': 'inline'});
				$(this).parent("div").parent("div").animate({'height':body_topic_height}, 600);
				$(this).unwrap();
				$(this).empty().append("Learn More &#187;");
				return false;
			}
		);
	}
	if($("#container_2").hasClass("contact")){	
	
		$("#send_a_message_button").click(function (){
			$("#contact_information").animate({
				marginLeft: '-400px'
			});				
			$("p#contact_greetings").css({
				'background-position' : '0px -138px',
				'cursor' : 'pointer'
			});
			
			$("p#contact_greetings").hover(
			   function(){
				   $(this).css({
						'background-position' : '0px -276px'									 
					});				   
				}, 
			   function(){
				   $(this).css({
						'background-position' : '0px -138px'									 
					});				   
				}
			);
						
			$("p#contact_greetings").click(function () {
				$("#country_feild").attr("value","");												 
				$("#contact_form").submit();
			});
			return false;
		});
		
		function input_runthrough () {
			$("#contact_form input").parent("p").css({'background-position': '0px 0px'});
			$("#contact_form input").css({'color':'#cacaca'});
			$("#contact_form input").each(function(){
				if($(this).attr("value") == " " || $(this).attr("value") == "" || $(this).attr("value") == "  ") {								   
					this_input_val = $(this).attr("alt");
					this.value = this_input_val;
				}
			});
		}
		function texarea_runthrough () {
			$("#contact_form textarea").parent("p").css({'background-position': '0px 0px'});
			$("#contact_form textarea").css({'color':'#cacaca'});
			$("#contact_form textarea").each(function(){
				if($(this).html() == " " || $(this).html() == "" || $(this).html() == "  ") {								   
					this_input_val = $(this).attr("label");
					$(this).html(this_input_val);
				}
			});
		}

		$("#contact_form input").focus(function (){
			texarea_runthrough();
			input_runthrough();
			this_input_alt = $(this).attr("alt");
			this_input_val = $(this).attr("value");
			if (this_input_val == this_input_alt) {
				this.value=' ';
			}
			$(this).css({'color':'#2f2f2f'});
			$(this).parent("p").css({'background-position' : '-400px 0px'});
		});
		$("#contact_form textarea").focus(function (){
			texarea_runthrough();
			input_runthrough();
			this_input_alt = $(this).attr("label");
			this_input_val = $(this).html();
			if (this_input_val == this_input_alt) {
				$(this).html(' ');
			}
			$(this).css({'color':'#2f2f2f'});
			$(this).parent("p").css({'background-position' : '-400px 0px'});
		});
		
	}
});

