$(document).ready(function () {
	var mOption = $("#blue-01").attr('class');
	var logoClass = $("#marketing-logo").attr('class');
	var defaultPitch = '#pitch_' + mOption;
	var tmpDefaultBanner =  $("#marketing").attr('class').split(" ");
	var defaultBanner = tmpDefaultBanner[2];
	var currentSelected = "";
	var page = $.url.attr("path");
	var page = page.replace(/\/+/g, ""); //just get the page name
	var anchortag = $.url.attr("anchor");
        var introStopped = false;
	//alert('test');
	$(window).load(function() {
		if( page == 'prosuite' || page == '' || page == 'consulting' || page == 'training' || mOption == 'custombanner' ){

			$(".marketing-" + mOption).animate({"opacity" : "1"},1500);
			$("#blue-02").animate({"left": "0", "top":"0"}, "slow");
			$("#blue-03").delay(500).animate({"left": "-=2000px"}, "slow");
			$("#blue-04").delay(1200).animate({"top": "+=2000px"}, "slow");
			$("#pitch_" + mOption).delay(2000).animate({opacity:"1"},1500);
			
			if ($.browser.msie) {
				$("#pitch_" + mOption).delay(1).animate({filter:"auto !important"},1500);
			 }						
						
                        //added logoClass. If moved mouse too quickly the class never got added
			$("#marketing-logo").addClass(logoClass);
                        $("#marketing a.logo_" + mOption).delay(2500).fadeIn(1200);
                        
			$("#" + mOption + "-graphics").delay(3000).animate({"opacity": "+=1", "right": "0", "top": "0"}, "slow");
		}

		if( page == '' ){ //means home page
			$("#ctablock a." + mOption + " .highlight").delay(3500).animate({"opacity": "+=1"}, "slow");
			$("#ctablock a." + mOption + " .highlight").delay(2000).animate({"opacity": "-=1"}, "slow");
		}
		if( page == 'smesolutions' ){

			anchortag = $.url.attr("anchor");
			if( anchortag != null ){
				handleBySMESubPages(anchortag);
			}
		}
	});
	if( page == '' ){
		//handling the logo
		$("#navigation li.smesolutions, #navigation li.consulting, #navigation li.training, .module.smesolutions, .module.consulting, .module.training")
			.mouseenter( function(){
				removeAllLogoClass();
				$("#marketing-logo").addClass("logo_smesolutions");

			})
			.mouseleave( function(){
				removeAllLogoClass();
				$("#marketing-logo").addClass(logoClass);
			})
			;
		$("#navigation li.prosuite, .module.prosuite")
			.mouseenter( function(){
				removeAllLogoClass();
				$("#marketing-logo").addClass("logo_prosuite");

			})
			.mouseleave( function(){
				removeAllLogoClass();
				$("#marketing-logo").addClass(logoClass);
			})
			;
		//prosuite
		$("#navigation li.prosuite, .module.prosuite")
			.mouseenter( function(){
				currentSelected = 'prosuite';
				var others = [ "smesolutions", "consulting", "training", "custombanner" ];


                               handleGraphicStripeOnMouseEnter('prosuite', others);
                                //$("#ctablock a.smesolutions a.consulting .highlight").stop();
                               // $("#ctablock a.smesolutions a.consulting .highlight").animate({"opacity": "0"}, 0);
                                handleBoxHoverOnMouse(others);
				$("#ctablock a.prosuite .highlight").animate({"opacity": "1"}, "slow");
				handlePitchOnMouseEnter('prosuite', others);
				handleBannerOnMouseEnter('prosuite', others);


			})
			.mouseleave( function(){

				handleGraphicStripeOnMouseLeave('prosuite');
				$("#ctablock a.prosuite .highlight").animate({"opacity": "0"}, 0);
				//handle pitch message
				handlePitchOnMouseLeave('prosuite');
				handleBannerOnMouseLeave();
                                //$this.stop();
                                //$("a.prosuite").stop();
			})
			;
		//smesolution

		$("#navigation li.smesolutions, .module.smesolutions")
			.mouseenter( function(){
				currentSelected = 'smesolutions';
				others = [ "prosuite", "consulting", "training", "custombanner" ];

				handleGraphicStripeOnMouseEnter('smesolutions', others);

                               // $("#ctablock a.prosuite a.consulting .highlight").stop();
                               // $("#ctablock a.prosuite a.consulting .highlight").animate({"opacity": "0"}, 0);
                                handleBoxHoverOnMouse(others);
				$("#ctablock a.smesolutions .highlight").animate({"opacity": "1"}, "slow");

				handlePitchOnMouseEnter('smesolutions', others);
				handleBannerOnMouseEnter('smesolutions', others);
                               // handleBoxHoverOnMouse('smesolutions', others);
			})
			.mouseleave( function(){
				handleGraphicStripeOnMouseLeave('smesolutions');
				$("#ctablock a.smesolutions .highlight").animate({"opacity": "0"}, 0);

				//handle pitch message
				handlePitchOnMouseLeave('smesolutions');
				handleBannerOnMouseLeave();
			})
			;
		//consulting
		$("#navigation li.consulting, .module.consulting")
			.mouseenter( function(){
				currentSelected = 'consulting';
				others = [ "prosuite", "smesolutions", "training", "custombanner" ];

				handleGraphicStripeOnMouseEnter('consulting', others);
                                
                              // $("#ctablock a.prosuite a.smesolutions .highlight").stop();
                              // $("#ctablock a.prosuite a.smesolutions .highlight").animate({"opacity": "0"}, 0);
                               handleBoxHoverOnMouse(others);
				$("#ctablock a.consulting .highlight").animate({"opacity": "1"}, "slow");
				handlePitchOnMouseEnter('consulting', others);
				handleBannerOnMouseEnter('consulting', others);
                                
			})
			.mouseleave( function(){
				handleGraphicStripeOnMouseLeave('consulting');
				$("#ctablock a.consulting .highlight").animate({"opacity": "0"}, 0);

				//handle pitch message
				handlePitchOnMouseLeave('consulting');
				handleBannerOnMouseLeave();
			})
			;
		//training
		$("#navigation li.training")
			.mouseenter( function(){
				currentSelected = 'training';
				others = [ "prosuite", "smesolutions", "consulting", "custombanner" ];
				handleGraphicStripeOnMouseEnter('training', others);
				handlePitchOnMouseEnter('training', others);
				handleBannerOnMouseEnter('training', others);
                                
			})
			.mouseleave( function(){
				handleGraphicStripeOnMouseLeave('training');
				//handle pitch message
				handlePitchOnMouseLeave('training');
				handleBannerOnMouseLeave();
			})
			;


		$("#ctablock .inside, #navigation")
			.mouseleave( function(){
				var others = [ "smesolutions", "consulting", "training", "prosuite", "custombanner" ];

				//default graphic stripe

				$.each(others, function() {
					$("#" + this + "-graphics").animate({"opacity": "-=1", "right": "-310px", "top": "310px"}, "fast");
				});

				$("#" + mOption + "-graphics").animate({"opacity": "1", "right": "0", "top": "0"}, "fast");


				//default banner
				$.each(others, function() {
					$("#marketing").removeClass("marketingbanner-" + this);
				});
				$("#marketing").addClass(defaultBanner);

				//default marketing pitch
				$.each(others, function() {
				$("#pitch_" + mOption).stop();
                                        $("#pitch_" + this).stop(); //added 7-5-10
					$("#pitch_" + this).css("opacity", "0");
					
			if ($.browser.msie) {
                                        $("#pitch_" + this).stop(); //added 7-5-10
					$("#pitch_" + this).css("filter", "alpha(opacity=0)");
			 }						
					//$("#pitch_" + this).css("filter", "alpha(opacity=0)");
				});
				$("#pitch_" + mOption).stop();
				$(defaultPitch).delay(300).animate({"opacity": "1"}, 1500); //ERROR FOUND
				
			if ($.browser.msie) {
                $(defaultPitch).stop(); //added 7-5-10
				$(defaultPitch).delay(1).animate({"filter": "auto !important"}, 1500); //ERROR FOUND
			 }						
				
				//$(defaultPitch).delay(1).animate({"filter": "alpha(opacity=0)"}, 1500); //ERROR FOUND
			})
                        /*.mouseenter( function(){
				//alert('success');
                                //var others = [ "smesolutions", "consulting", "training", "prosuite", "custombanner" ];
                                //$.each(others, function() {
				//	$("#" + this + "-graphics").stop();
                               // alert('hi');
                               var others = [ "smesolutions", "consulting", "training", "prosuite", "custombanner" ];
                               $.each(others, function() {
					$("#" + this + "-graphics").stop();
                                        $("#" + this + "-graphics").css("opacity", "0");
				});
                                        
				});*/
			//})
			;
	}

	/********** sme solutions 3 main block styling *************/
	$(".module.retail")
		.mouseenter( function(){
                       $("#headernav a.retail .highlight").animate({"opacity": "1"}, "slow");

		})
		.click( function(){
			$("#headernav a.retail .highlight").animate({"opacity": "1"}, "slow");

		})
		.mouseleave( function(){
			tmpAnchor = $.url.attr("anchor");
			if( tmpAnchor != 'retail' ){
				$("#headernav a.retail .highlight").animate({"opacity": "0"}, "slow");
			}
		})
		;

	$(".module.manufacturing")
		.mouseenter( function(){
                        $("#headernav a.manufacturing .highlight").animate({"opacity": "1"}, "slow");
		})
		.click( function(){
			$("#headernav a.manufacturing .highlight").animate({"opacity": "1"}, "slow");
		})
		.mouseleave( function(){
			tmpAnchor = $.url.attr("anchor");
			if( tmpAnchor != 'manufacturing' ){
				$("#headernav a.manufacturing .highlight").animate({"opacity": "0"}, "slow");
			}
		})
		;

	$(".module.distribution")
		.mouseenter( function(){
                        $("#headernav a.distribution .highlight").animate({"opacity": "1"}, "slow");
                        //kill other hover actions


		})
		.click( function(){
			$("#headernav a.distribution .highlight").animate({"opacity": "1"}, "slow");
		})
		.mouseleave( function(){
			tmpAnchor = $.url.attr("anchor");
			if( tmpAnchor != 'distribution' ){
				$("#headernav a.distribution .highlight").animate({"opacity": "0"}, "slow");
			}
		})
		;
	/********** end sme solutions 3 main block styling *************/


	/********* handle sme solution sub pages (retain, manufacturing, distribution) ***********/

	$(".module.retail").click(function(){
		handleSMESubpages('retail', ['manufacturing', 'distribution']);
	});
	$(".module.manufacturing").click(function(){
		handleSMESubpages('manufacturing', ['retail', 'distribution']);
	});
	$(".module.distribution").click(function(){
		handleSMESubpages('distribution', ['retail', 'manufacturing']);
	});
	$(".module-smesolutions-manufacturing a.icon-minus, .module-smesolutions-distribution a.icon-minus, .module-smesolutions-retail a.icon-minus").click(function(){
		$('.module-smesolutions-retail').hide("slow");
		$('.module-smesolutions-manufacturing').hide("slow");
		$('.module-smesolutions-distribution').hide("slow");
	});

	$("#navigation li.smesolutions ul.subnav a").click(function(){
		hash = $.url.setUrl($(this).attr("href")).attr("anchor");
		handleBySMESubPages(hash);
	});



	function handleSMESubpages(showPage, hidePages){

		$.each(hidePages, function() {
			$(".module-smesolutions-" + this).hide("slow");
			$("#headernav a." + this + " .highlight").animate({"opacity": "0"}, "slow");
		});
		$('.module-smesolutions-' + showPage).show("slow");
		$( "#accordion-smesolutions" ).accordion("activate", -1);
	}
	function handleBySMESubPages(anchortag){
		if( anchortag == 'retail' ){
			$("#headernav a.retail .highlight").animate({"opacity": "1"}, "slow");
			handleSMESubpages('retail', ['manufacturing', 'distribution']);
		}
		else if( anchortag == 'manufacturing' ){
			$("#headernav a.manufacturing .highlight").animate({"opacity": "1"}, "slow");
			handleSMESubpages('manufacturing', ['retail', 'distribution']);
		}
		else if( anchortag == 'distribution' ){
			$("#headernav a.distribution .highlight").animate({"opacity": "1"}, "slow");
			handleSMESubpages('distribution', ['retail', 'manufacturing']);
		}
	}
	/******** handle sme solution sub pages (retain, manufacturing, distribution) ***************/


	/******* control more/less button ***********/
	var more_button = { "more-cistech-btn-retail" : "#cistech-content-retail",
							"more-batchmaster-btn-manufacturing" : "#batchmaster-content-manufacturing",
							"more-trimergo-btn-manufacturing" : "#trimergo-content-manufacturing",
							"more-procubes-btn-distribution" : "#procubes-content-distribution",
							"more-ibolt-btn-integration" : "#ibolt-content-integration",
							"more-sqlserver-btn-integration" : "#sqlserver-content-integration",
							"more-taskcentre-btn-integration" : "#taskcentre-content-integration",
							"more-crystal-btn-reporting" : "#crystal-content-reporting",
							"more-excel-btn-reporting" : "#excel-content-reporting",
							"more-procubes-btn-reporting" : "#procubes-content-reporting",
							"more-excel-btn-consulting" : "#excel-content-consulting",
							"more-mysql-btn-consulting" : "#mysql-content-consulting",
							"more-outlook-btn-consulting" : "#outlook-content-consulting",
							"less-cistech-btn-retail" : "#cistech-content-retail",
							"less-batchmaster-btn-manufacturing" : "#batchmaster-content-manufacturing",
							"less-trimergo-btn-manufacturing" : "#trimergo-content-manufacturing",
							"less-procubes-btn-distribution" : "#procubes-content-distribution",
							"less-ibolt-btn-integration" : "#ibolt-content-integration",
							"less-sqlserver-btn-integration" : "#sqlserver-content-integration",
							"less-taskcentre-btn-integration" : "#taskcentre-content-integration",
							"less-crystal-btn-reporting" : "#crystal-content-reporting",
							"less-excel-btn-reporting" : "#excel-content-reporting",
							"less-procubes-btn-reporting" : "#procubes-content-reporting",
							"less-excel-btn-consulting" : "#excel-content-consulting",
							"less-mysql-btn-consulting" : "#mysql-content-consulting",
							"less-outlook-btn-consulting" : "#outlook-content-consulting"
							};
				$("#cistech-content-retail,#batchmaster-content-manufacturing,#trimergo-content-manufacturing,#procubes-content-distribution,#ibolt-content-integration,#sqlserver-content-integration,#taskcentre-content-integration,#crystal-content-reporting,#excel-content-reporting,#procubes-content-reporting,#excel-content-consulting,#mysql-content-consulting,#outlook-content-consulting").hide();

				$("#more-cistech-btn-retail,#more-batchmaster-btn-manufacturing,#more-trimergo-btn-manufacturing,#more-procubes-btn-distribution,#more-ibolt-btn-integration,#more-sqlserver-btn-integration,#more-taskcentre-btn-integration,#more-crystal-btn-reporting,#more-excel-btn-reporting,#more-procubes-btn-reporting,#more-excel-btn-consulting,#more-mysql-btn-consulting,#more-outlook-btn-consulting").click(function(){
					$(more_button[$(this).attr('id')]).show('slow');
					return false;
				});

				$("#less-cistech-btn-retail,#less-batchmaster-btn-manufacturing,#less-trimergo-btn-manufacturing,#less-procubes-btn-distribution,#less-ibolt-btn-integration,#less-sqlserver-btn-integration,#less-taskcentre-btn-integration,#less-crystal-btn-reporting,#less-excel-btn-reporting,#less-procubes-btn-reporting,#less-excel-btn-consulting,#less-mysql-btn-consulting,#less-outlook-btn-consulting").click(function(){
			$(more_button[$(this).attr('id')]).hide('slow');
			return false;
			});
		/******* end control more/less button ***********/




	//handling graphic stripe
	function handleGraphicStripeOnMouseEnter(selectedProd, others){
		$.each(others, function() {
                    // added by alex
                    $("#" + this + "-graphics").stop(); 
                    $("#" + this + "-graphics").animate({"opacity": 0}, 0);
		    //$("#" + this + "-graphics").animate({"opacity": "0", "right": "-310px", "top": "310px"}, "fast");

		});
		$("#" + selectedProd + "-graphics").animate({"opacity": "1", "right": "0", "top": "0"}, "fast");
	}
       

	function handleGraphicStripeOnMouseLeave(selectedProd){

		if( currentSelected != selectedProd && currentSelected != mOption){

			$("#" + selectedProd + "-graphics").animate({"opacity": "-=1", "right": "-310px", "top": "310px"}, "fast");
		}
	}

        function stopIntroAnimation()
        {
           
        }
        function handleBoxHoverOnMouse(others)
        {
           // alert('handle called');

           if(!introStopped)
             {
                //alert('new page');
                $("#marketing a.logo_" + mOption).stop();
                $("#marketing a.logo_" + mOption).fadeIn(0);
                $("#ctablock a." + mOption + " .highlight").stop();
                $("#ctablock a." + mOption + " .highlight").animate({"opacity": 0}, 0);
                introStopped = true;
             }

            $.each(others, function() {
                  $("#ctablock a."+this+ " .highlight").stop();
                  $("#ctablock a."+this+ " .highlight").animate({"opacity": 0}, 0);
         });

        }
	//handling main big banner
	function handleBannerOnMouseLeave(){
		$("#marketing").addClass(defaultBanner);
	}
	function handleBannerOnMouseEnter(selectedProd, others){
		$.each(others, function() { //ADD HERE
                    $("#marketing").removeClass("marketingbanner-" + this);

		});
		$("#marketing").addClass("marketingbanner-" + selectedProd);
	}

	//handling marketing message
	function handlePitchOnMouseLeave(selectedProd){

		if( selectedProd != currentSelected && currentSelected != mOption ){
			$("#pitch_" + selectedProd).css("opacity","0");
		}
		else if( selectedProd != currentSelected ){
			$("#pitch_" + selectedProd).css("opacity","0");
		}

	}
	function handlePitchOnMouseEnter(selectedProd, others){
		//hide the rest
               // stopIntroAnimation();
                $(defaultPitch).stop();
		$.each(others, function() {
				$("#pitch_" + mOption).stop();
                   $("#pitch_" + this).stop();
		   $("#pitch_" + this).animate({"opacity": 0}, 0);
		
			if ($.browser.msie) {
                                        $("#pitch_" + this).stop(); //added 7-5-10
		   $("#pitch_" + this).animate({"filter": "alpha(opacity=0)"}, 0);
			 }						
		
		   //$("#pitch_" + this).animate({"filter": 0}, 0);

		});
		//handle pitch message
				$("#pitch_" + mOption).stop();
		$("#pitch_" + selectedProd).animate({"opacity": "1"}, 300); //show this message
		
			if ($.browser.msie) {
                                        $("#pitch_" + this).stop(); //added 7-5-10
		$("#pitch_" + selectedProd).animate({"filter": "auto !important"}, 300); //show this message
			 }						
		//$("#pitch_" + selectedProd).animate({"filter": "alpha(opacity=0)"}, 300); //show this message
	}

        //function

	function removeAllLogoClass(){
		$("#marketing-logo").removeClass("logo_prosuite");
		$("#marketing-logo").removeClass("logo_consulting");
		$("#marketing-logo").removeClass("logo_training");
		$("#marketing-logo").removeClass("logo_smesolutions");
		$("#marketing-logo").removeClass("logo_custombanner");
	}

	});

