$(function( ) {
	// engines of the user
	engine.init( user );
	// panel promos
	if ( promo.toLowerCase( ) === "y" ) {
		$( "<div/>" ).attr( { "id": "arrow-2", "title": (zbp === "n") ? key.panelShow : key.panelHide } ).addClass( zbp === "n" ? "qla242424up" : "qla242424" ).insertAfter( "#arrow-1" ).click( togglePromo ).bind( "customAnimate", animateArrowPromo );
		if ( zbq.toLowerCase( ) === "n" && zbp.toLowerCase( ) === "y" ) {
			togglePromo( true );
		}
	}
	// quick cont open
	$( "#arrow-1" ).attr( "title", zbq === "n" ? key.panelShow : key.panelHide ).click( toggle ).bind( "customAnimate", animateArrow );
	if ( zbq.toLowerCase( ) === "y" ) {
		toggle( true );
	}
	// wallpaper
	if ( wp ) {
		$("#wrapper").css({backgroundImage:"url("+ wp +")"});
	}
	// submit
	$( "a.round" ).hover( over, out ).click( send );
	// wallpaper
	//if ( typeof wp === "string" && wp.length < 1 ) {
		//$( "<div/>" ).attr({ id:"box-peel-me" }).append( $( "<div/>" ).attr( "id", "box-inside-peel-me" ).append( $( "<span/>" ).addClass( "cursor" ).attr( "id", "peel-me" ).append( $( "<img/>" ).attr( "src", general.baseUrl+"/images/peel-me.gif" ) ) ) ).prependTo( "#main" ).bind( "click", loadWallpaper );
        $( "<div/>" ).attr({ id:"box-peel-me" }).append( $( "<div/>" ).attr( "id", "box-inside-peel-me" ).append( $( "<span/>" ).addClass( "cursor" ).attr( "id", "peel-me" ) ) ).prependTo( "#main" ).bind( "click", loadWallpaper );
        $("#peel-me").hide();
        $("#box-peel-me").hover(
            function(){
                $("#peel-me").html("");
                $("#peel-me").append( $( "<img/>" ).attr( "src", general.baseUrl+"/images/peel-me.gif" ));
                $("#peel-me").show();
            },
            function() {
                $("#peel-me").hide();
            }
        );
	//}
	// date && flag && copy
	if ( showDate.toLowerCase( ) === "y" ) {
		$( "<div/>" ).attr( "id", "date" ).prependTo( "#menu" ).html( general.getNow( true ) );
	}
	$( "#copy" ).html( general.getCopyright( true ) );
	general.getFlag();
	// set Home Page link
	createHomePageLink();
	function send( ) {
		var q = register.clear( $( "#searchfield" ).val( ) ) || "";
    if (q.length > 0) {
      log.q( );
      $( "#cse-search-box" ).submit();
    } else {
      $( "#searchfield" ).val( "" ).trigger( "focus" );
    }
	};
	function over( ) {
		$( this ).next( ).addClass( "pasroundhover" );
	};
	function out( ) {
		$( this ).next( ).removeClass( "pasroundhover" );
	};
	function toggle( cancel ) {
		var clasInv = ( theme == "242424" || theme == "069" || theme== "00991b" ) ? "inv" : "",
			content = $( "#qlinks" ).is( ":visible" ) || $( "#qlinks" ).is( ":hidden" ) ? $( "#qlinks" ) : $( "<div/>" ).attr( "id", "qlinks" ).css( "background", "url("+general.baseUrl+"/images/themes/" + theme + ".gif) repeat-x" ).append( $( "<div/>" ).attr( "id", "qltitle" ).addClass( clasInv ).html( "Quick Links" ) ).css( "display", "none" ).bind( "customAnimate", animateContent );
		if ( !$( "#qlinks" ).is( ":visible" ) && !$( "#qlinks" ).is( ":hidden" ) ) {
			content.insertAfter( "#arrow-1" );
			loadQuickResources( );
		}
		content.add( "#arrow-1" ).trigger( "customAnimate" );
		if ( typeof cancel !== "boolean" ) {
			setPanel( "quick_cont_open", parseInt( $( "#arrow-1" ).css( "marginTop" ), 10 ) === -41 ? "y" : "n" );
		}
		function animateContent( ) {
			content.slideToggle( "normal" );
			setArrowPosition( );
			setPromoPosition( );
			function setArrowPosition( ) {
				$( "#arrow-2" ).animate( { "marginTop": parseInt( $( "#arrow-1" ).css( "marginTop" ), 10 ) === -41 ? "-=100" : "+=100" }, "normal", callback );
				function callback(  ) {
					if ( !$( "#promos" ).is( ":visible" ) && !$( "#promos" ).is( ":hidden" ) && zbp.toLowerCase( ) === "y" ) {
						togglePromo( true );
					}
				}
			}
			function setPromoPosition( ) {
				$( "#promos" ).animate( { "bottom": parseInt( $( "#arrow-1" ).css( "marginTop" ), 10 ) === -41 ? "+=100" : "-=100" }, "normal" );
			}
		}
	};
	function animateArrow( ) {
		var margin = parseInt( $( "#arrow-1" ).css( "marginTop" ), 10 ),
			top = margin === -41 ? -141 : -41,
			index = margin === -41 ? "-=100" : "+=100";
		$( "#arrow-1" ).attr( "title", top === -41 ? key.panelShow : key.panelHide ).unbind( "click", toggle ).removeClass( ).addClass( top === -141 ? "qla" + theme : "qla" + theme + "up" ).animate({ "marginTop": index }, "normal", function( ){ $( this ).bind( "click", toggle ) } );
		$( "#sarea, #srchrow" ).animate( { "marginTop": margin === -41 ? "-=25" : "+=25" }, "normal" );
    $( "#cntEngXtra" ).animate( { "marginTop": margin === -41 ? "-=50" : "+=50" }, "normal" );
	};
	function setPanel( conf, value ) {
		$.ajax({
			async : true,
			global : false,
			url :  "ajax/set-conf.php",
			data : { "id": conf, "value": value },
			type : "get",
			dataType: "json"
		});
	};
	function loadQuickResources( ) {
		$.getScript( general.baseUrl+"/scripts/module.quick.1.2.js", function( ){ quicklink.load( loadQuicklinks, geoip ); });
	};
	function animateArrowPromo( ) {
		var margin = parseInt( $( "#arrow-2" ).css( "marginTop" ), 10 ),
			top = ( margin === -41 || margin === -141 ) ? "-=60" : "+=60";
		$( "#arrow-2" ).attr( "title", top === "+=60" ? key.panelShow : key.panelHide ).unbind( "click", togglePromo ).removeClass( ).addClass( top === "-=60" ? "qla242424" : "qla242424up" ).animate({ "marginTop": top }, "normal", function( ){ $( this ).bind( "click", togglePromo ) } );
		$( "#srchrow, #sarea" ).animate( { "marginTop": ( margin === -41 || margin === -141 ) ? "-=25" : "+=25" }, "normal" );
    $( "#cntEngXtra" ).animate( { "marginTop": ( margin === -41 || margin === -141 ) ? "-=50" : "+=50" }, "normal" );
	};
	function togglePromo( cancel ) {
		var bottom = parseInt( $( "#arrow-1" ).css( "marginTop" ), 10 ) === -41 ? 25 : 125,
			content = $( "#promos" ).is( ":visible" ) || $( "#promos" ).is( ":hidden" ) ? $( "#promos" ).css( "bottom", bottom ) : $( "<div/>" ).attr( "id", "promos" ).load( "ajax/get-promo.php" ).css( { "display": "none", "bottom": bottom } ).bind( "customAnimate", animateContent );
		if ( !$( "#promos" ).is( ":visible" ) && !$( "#promos" ).is( ":hidden" ) ) {
			content.insertAfter( "#arrow-2" );
		}
		content.add( "#arrow-2" ).trigger( "customAnimate" );
		if ( typeof cancel !== "boolean" ) {
			setPanel( "promos_cont_open", ( parseInt( $( "#arrow-2" ).css( "marginTop" ), 10 ) === -41 || parseInt( $( "#arrow-2" ).css( "marginTop" ), 10 ) === -141 ) ? "y" : "n" );
		}
		function animateContent( ) {
			content.slideToggle( "normal" );
		}
	};
	function loadWallpaper( ) {
		callbackModal( );
		function create( ) {
			var elem = $( "#cntc" );
			$( "<span/>" ).attr( "id", "modal-close" ).addClass( "cursor" ).append( $( "<img/>" ).attr( "src", general.baseUrl+"/images/modal-close.gif" ) ).insertBefore( elem ).click( register.close );
			$( "<p/>" ).addClass( "ttl-modal" ).insertBefore( elem ).html( key.setwallp + "<br />" );
			$( "<div/>" ).attr( "id", "cntQuickHome" ).insertBefore( elem ).html( loadSelect( "cntQuickHome" ) );
			//$( "<div/>" ).attr( "id", "msgsavewallpaper" ).appendTo( elem );
		}
		function callbackModal( ) {
			if ( typeof $.modal === "function" ) {
				showModal( );
			} else {
				$.getScript( general.baseUrl+"/scripts/jquery.simplemodal.1.2.js", showModal );
			}
			function showModal( ) {
				var modalLeft = ( $( window ).width( ) - 765 ) / 2,
					modalTop = $( window ).height( ) / 4;
				$( "#cntc" ).modal({
					containerCss: {
						width: "800px"
					},
					overlayId: "noteOverlay",
					containerId: "setQuicklinks",
					position:[ modalTop, modalLeft ],
					onShow : function( ) {
                        $("div.scrollable").scrollable();
						$( "div#noteOverlay" ).click ( register.close );
						callbackCombo( );
					}
				});
			}
		}
		function callbackCombo( ) {
			if ( typeof setWallpaper === "function" ) {
				create( );
			} else {
				$.getScript( general.baseUrl+"/scripts/module.wall.js", create );
			}
		}
	}
	function createHomePageLink() {
		//if (general.isFx) alert('firefox'); else alert('no firefox');
		var cookie = _cookie.get("shp"),
		span = $("<span/>").attr({id:"sethomepage"}).css({behavior:"url(#default#homepage)"}).appendTo($("body")),
		el = document.getElementById("sethomepage"),
		browser = "",
		expire,
		linkval = key.shp1;
		if (general.isIE7) {browser = general.baseUrl + '/images/homeie7.gif';}
		else if (general.isIE6) {browser = general.baseUrl + '/images/homeie6.gif';}
		else {browser = general.baseUrl + '/images/home.gif'; linkval += ' '+key.shp2; expire = 90;}
		if (general.isIE6 || general.isIE7) { var already = el.isHomePage("http://www.todobusque.com") || el.isHomePage("http://www.todobusque.com/"); }
		if (general.isIE6 || general.isIE7 || general.isFx) {
			if (cookie!="1" && typeof user != "undefined" && user * 1 === 0) {
				if (typeof already == "undefined" || already===false) {
					$("#box-inside-peel-me").css({marginTop:"52px"});
					$("<div/>").attr({id:"shpRow"}).prependTo("#main").html('<img src="'+ browser +'" alt="" class="homy" /><a href="http://www.todobusque.com">'+ linkval +'</a><img src="' + general.baseUrl + '/images/closebar.gif" alt="" id="cancel-shp" />');
					$("#cancel-shp").click(function(){
						_cookie.set("shp","1",expire);
						$("#shpRow").slideUp("normal");
						$("#box-inside-peel-me, #zombol-bar").animate({marginTop:"-=24"},"normal");
						return false;
					});
					$("#shpRow").not("#cancel-shp").click(function(){
						el.setHomePage("http://www.todobusque.com");
						$("#shpRow").slideUp("normal");
						$("#box-inside-peel-me, #zombol-bar").animate({marginTop:"-=24"},"normal");
						return false;
					});
				}
			}
		}
	};
});
