var isAnimStopped = true;
var isShake = false;
var  b;
var  aut;
var timer=0;
var isSet=0;

var currentLI = 0;
var lengthLI = 0;
/*
 * To redirect the site to 'search/search_string'
 * to get the search results
 */
//function submitSearch(){
//	var str = $('#search_txt').val();
//	var trimmed = str.replace(/^\s+|\s+$/g, '') ;
//	document.location = "/search/"+trimmed;
//}
/*
 * To show the related category projects in project slider
 * mac_development - is the hidden field id which contains the selected value from the drop down.
 */
function showCategoryProjects(){
	//alert('Hello..');
	var catIdClass = '.cat_'+$('#mac_development').val();
	$('#myslider tr td').hide();
	$('.grid-element').hide();
	if(catIdClass == '.cat_1' || catIdClass == '.cat_9' || catIdClass == '.cat_12'){
		$('#myslider tr td').fadeIn(1200);
		$('.grid-element').fadeIn(1200);
	}
	$(catIdClass).fadeIn(1200);
	$('.grid-view>'+catIdClass).fadeIn(1200);
}

function bubbleEffect(){
	var jqNode = $(".subscribe-input");
	if(isShake){ 
			jqNode.animate({top:"-=15px"},250,'easeOutQuad').animate({top:"+=15px"},250,'easeInQuad');
			timer++;			
		}
	/*
	if(timer<=3){
		b = setTimeout("bubbleEffect()", 1168);
	}else{
		isShake = false;
		clearTimeout(b);
		timer=0;
	}
	*/	
}

/*
 * To aninate the slider elements [group by group].
 * 160 - to slide one element
 * 160 * n [no of elements to animate]
 */
function slideCarousel(elm, sign) {
	var	currentProductId = 0;
	var no = parseInt(elm.css('left'));
	var nofElms = 160 * 5;

	isAnimStopped = false;
	if(sign == '+') { no += nofElms; }
	else { no -= nofElms; }
	if(no > 0) {
		elm.animate({left:"0px"}, 600, 'easeInQuad', function() {
			isAnimStopped = true;
			setSession(no);
		});	
	}else{
		elm.animate({left:no}, 600, 'easeInQuad', function() {
			isAnimStopped = true;
			setSession(no);
		});	
	}		
}


function autoPlay(){
	currentLI++;
	if(currentLI > lengthLI) { currentLI = 1; }
	
	var currTab = $('#service-menu li:nth-child('+currentLI+')').attr('id');
	$('#service-menu li').each(function(){
			$(this).removeClass('active');
		});
	$('#'+currTab).addClass('active');
	
	var currFeatProj = '#'+currTab+'_featured_proj';
	$('.content-inner>div').hide();
	$(currFeatProj).fadeIn(1900);
	aut = setTimeout("autoPlay()", 21000);
}

function selectRandomMenu(){
	// To select random tab menu 
    var randomnumber=Math.floor(Math.random()*3);
    randomnumber += 1;
    $('#service-menu li:nth-child('+randomnumber+')').addClass('active');
    var menuid = $('#service-menu li:nth-child('+randomnumber+')').attr('id');
    $('#'+menuid+'_featured_proj').fadeIn(1200);
}
$(document).ready(function(){
	
	$('.right').css({position:"relative",top:"0px",right:"0px"});
	
	var sto = setTimeout(function() {
		$('#sb_footer').append("<div id='cipI7l' style='z-index:100;position:absolute'></div><div id='scpI7l' style='display:inline'></div><div id='sdpI7l' style='display:none'></div><div style='display:inline'><a href='http://www.providesupport.com?messenger=sourcebits'>Customer Service</a></div></noscript>");
		var sepI7l = document.createElement('script');
		sepI7l.type='text/javascript';
		var sepI7ls=(location.protocol.indexOf('https')==0?'https':'http')+'://image.providesupport.com/js/sourcebits/safe-standard.js?ps_h=pI7l\u0026ps_t='+new Date().getTime()+'\u0026online-image=http%3A//sbnew.publishy.com/images/buttons/onlinechat.png\u0026offline-image=http%3A//sbnew.publishy.com/images/buttons/offlinechat.png';
		setTimeout(function() {
			sepI7l.src=sepI7ls;document.getElementById('sdpI7l').appendChild(sepI7l);	
		},1);
	}, 1000);
	
	
	//<script type='text/javascript'></script><noscript>
	
	/*	HOME Page: Services menu
     *  To retrive the featured project of the services.
     *  id is taken from the service menu li attribute [ex: "service_mac","service_web", etc..]
     *  The Project results are pre displayed in the homepage itself on page load.
     *  on cliking of the menu item the service id [ex: "service_mac"] is taken and added the
     *  "_featured_proj" which is project id for that related service. and displayed it
     *  in FADEIN effect.
     */
    //selectRandomMenu();
    
	$('#service-menu li').livequery('click',function(i){
		$('#service-menu li').each(function(){
			$(this).removeClass('active');
		});
		//console.log($(this).attr('name'));
		clearTimeout(aut);
		currentLI=($(this).attr('name'))-1;		
		//currentLI = 0;
		$(this).addClass('active');
		var featured_proj = $(this).attr('id')+'_featured_proj';
		$('.content-inner>div').hide();
		$('#'+featured_proj).fadeIn(500,function(){
			//autoPlay();
		});
	});
	
	// This is for service sub menu display on mouse hover on service tab menu in the header.
	/*
	$('#sub-menu').hover(function(){
		$(this).addClass('hover'); //chin
					
	},
	function(){
		$(this).removeClass('hover'); //chin		
	});
	
	$('#about_sub_menu').hover(function(){
		$(this).addClass('hover'); //chin					
	},
	function(){
		$(this).removeClass('hover'); //chin		
	});
	
	*/
	/*
	 * SERVICS Page:
	 * All projects are pre displayed and on clicking the project in slider the
	 * related project has been displayed using Fadein Effect
	 */
	$('.jcarousel-item').click(function(){
		var projId = $(this).attr('id');
		projId = '#'+projId+'_div';
		$('#project_container>div').hide();
		$(projId).fadeIn(1200);
	});

	// To initiate the drop down component.
	jQuery.TTcombo();

	/*
	 * Contactus Page: Message subject selection on cliking of the RIGHT SIDE links.
	 * msgid - this is the id of cliked link[ex: "request_quote", "apply_job"] given for li structure.
	 * title - to display the title for drop down [ex: "Requset a quote", "Apply for a job"].
	 * cont_input - to show the selected value for drop down list,
	 * cont - hidden input field [this is the main field to send the selected value on submit].
	 * cont_input_'request_quote' - this is for display current selected value in drop down list.
	 */
	 $('#msg_subject li').livequery('click',function(){
	 	$('#content_title').html($(this).attr("title"));
	 	var msgid = $(this).attr('id');
	 	var title = $(this).attr('title');
	 	$('#cont_input').val(title);
	 	$('#cont').val(title);
	 	$('.combo_list_item li').removeClass('selected');
	 	$('#cont_input_'+msgid).addClass('selected');
	 	hideCallBack();
	 });
	$('.reset').click(function(){
		$('div#main_form :input').val('');
		$('#cont_input').val('Choose a subject');		
	});
	/*
	 * To display the e-mail field on clicking the subscribe image in home page
	 */
//	$('.subscribe-email').click(function(){
//		$('#err').html('');
//		$('.subscribe-input').fadeIn(700,function(){
//			isShake = true;
//			bubbleEffect();
//		});		
//	});
	
	
//	$('#subscribe_submit').click(function(){
//		isShake = false;
//		clearTimeout(b);
//		$('.subscribe-input').fadeOut(200);
//	});
	
	$(".subscribe-email").livequery('click', function() {
		
		$('#err').html('');
		$('#subscribe').css({background: "none"});
	    var menu = $('#' + $(this).attr('id').replace('_id', '_input'));
	    if (/^none$/.test(menu.css('display'))) {
            $(".subscribe-input").fadeOut("fast");
            menu.fadeIn("fast");
            //$('#subscribe').focus();
            isShake = true;
			bubbleEffect();       	   
	    }else{
	    	isShake = false;
			clearTimeout(b);
	    	menu.fadeOut("fast");
	    }
		return false;
    });
    
    $('#subsc_email_input').livequery('click', function(e) {
    	if($.browser.msie){
    		window.event.cancelBubble = true;
    	}else{
    		e.stopPropagation();
    	}
    });
   
	$('.subscribe-input').livequery('click',function(){
		$(this).fadeIn('fast');
    });
	$(document).bind('click',function() {
        $(".subscribe-input").fadeOut("fast");
    });
    
	/*
	 * To display the GRID view.
	 */
	$('#grid_icon').click(function(){
		//$('#myslider').css({left: "0px"});
		$('#gal_view').hide();
		//$('#gal_view').css({height:"0px",overflow:"hidden"});
    	//$('.thumbs').css({height:"0px",overflow:"hidden"});
		$('.grid-view').fadeIn(1200);
		// This wil change the grid icon and text.
		$(this).hide();
		$('#gal_icon').fadeIn(1200);
		$('#icon_text').html('Gallery View');
	});

	/*
	 * To display the GALLERY view.
	 */
	$('#gal_icon').click(function(){
		//$('#myslider').css({left: "0px"});
		$('.grid-view').hide();
		$('#gal_view').fadeIn(1200);
		$('.launchgallery').css({opacity:"1",filter:"alpha(opacity=100)"});
		//$('#gal_view').css({height:"100%",overflow:""});
		//$('.thumbs').css({height:"150px",overflow:""});
		$(this).hide();
		// This wil change the gallery icon and text.
		$('#grid_icon').fadeIn(1200);
		$('#icon_text').html('Grid View');
		$('#gal_view').css({height:"200px",overflow:""});
		$('.thumbs').css({height:"150px",overflow:""});			
        
		if(isSet==0){
			setCarousel();
			isSet=1;
		}		
		
	});

	/*
	 * To slide the elements to right [PREVIOUS]
	 */
//	$('#scroll_previous').click( function() {
//		var elm = $('#myslider');
//		var no = parseInt(elm.css('left'));
//		if(no == 0) { return false; }
//		if(isAnimStopped) {
//			slideCarousel(elm, '+');
//		}
//	});
//
//	/*
//	 * To slide the elements to left [NEXT]
//	 */
//	$('#scroll_next').click( function() {
//		var elm = $(this).parent().parent().find('div.thumbs-body').find('table');
//		if((Math.abs(parseInt(elm.css('left')))+334+160+160+160+160) >= elm[0].offsetWidth) { return false; }
//		if(isAnimStopped) {
//			slideCarousel(elm, '-');
//		}
//	});

	/*
	 * CONTACT Page : Requst callback fields display
	 */
	 $('#callback_btn').click(function(){
	 	$('#success_msg_contact').addClass('hide');
	 	$('#content_title').html('Request a Callback');
	 	showCallBack();
	 });
	 $('.close-icon').click(function(){
	 	$('#content_title').html('Contact us');
	 	hideCallBack();
	 });
	 
	 /* chin.... */
	 
	 $(document).ready(function(){
    
	    // Using multiple unit types within one animation.
	    /*
	    $("#home_sub_menu").mouseover(function(){
	      $(this).fadeOut( 1000 );
	    });
		$("#home_sub_menu").mouseout(function(){
	      $(this).fadeIn( 1000 );
	    });
	   */ 
	   
	   //"#home_sub_menu, #sub-menu, #about_sub_menu, #blog_sub_menu, #contact_sub_menu").hover(
	  	$("#sub-menu, #about_sub_menu").hover(
	  		function(){
	  			if($(this).hasClass('active')){
	  				$(this).addClass('hover');
		  	  		$(this).fadeIn(500);
	  			}else{
	  				$(this).hide();
	  				$(this).addClass('hover');
		  	  		/*$(this).fadeIn(500);*/
	  			}
	  					  	  
	  		},
	  		function() {	  			  		
		  		//$(this).fadeOut('fast');
		  		$(this).removeClass('hover');
		  		/*$(this).fadeIn('normal');*/

	  		}
	  	);

	  });
});
