﻿var homePage = function($){
    var priv = {
          bindPromoBlockEvents  : function(){
            $("#homepagerighttop div.promo-block li").bind("click",
                function(){
                    document.location.href = $(this).find("a").attr("href");
                }
            ).hover(
                function(evt){
                    $(this).addClass("hover");
                },
                function(evt){
                    $(this).removeClass("hover");
                }
            );
          },
          
          writeFlashBanner : function() {
            try {
                var flashWidth = 437;
                var flashHeight = 174;
                var flashFile = resources.skin_path + $('#brochure-flash span').text() + $('#flashParams').text();
                $('#brochure-flash').html('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="'+flashWidth+'" height="'+flashHeight+'">'+
                    '<param name="allowScriptAccess" value="sameDomain" />'+
                    '<param name="movie" value="'+flashFile+'" />'+ 
                    '<param name="quality" value="high" />'+
                    '<param name="bgcolor" value="#ffffff" />'+
                    '<embed src="'+flashFile+'" quality="high" bgcolor="#ffffff" width="'+flashWidth+'" height="'+flashHeight+'" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />'+
                '</object>');
                
                $('#brochure-flash object').get(0).outerHTML = $('#brochure-flash object').get(0).outerHTML;
             }
             catch(err){
                //nice catch
             }
          },
          
         inputSubmit : function(){  
             if($('#txtsearchbox').val() != null && $('#txtsearchbox').val() != ""){
                //replace the "/" with a space
                var val = $('#txtsearchbox').val();
                val = val.replace("/", " ");
                document.location.href = resources.path_prefix + '/search.aspx?searchtext='+val; 
            }
         },
         
         bindCountryLocations    : function(){
		    var $countryPopup = $("div#country-popup");
		    $("li.show-country-popup").hover(
		        function(){
		            $(this).addClass("country-selected");
		            $(this).append($countryPopup);
		            $countryPopup.show();
		            if ($(this).parent().parent().hasClass("left")) {
		               $("div#country-popup").css("left","325px");
		            }
		            if ($(this).parent().parent().hasClass("right")) {
		               $("div#country-popup").css("left","450px");
		            }
		            return false;
		        },
		        function(){
		            $(this).removeClass("country-selected");
		            $countryPopup.hide();
		            return false;
		        }
		    )
	        $("a.remove").click(
	            function(){
	                $countryPopup.hide();
	                return false;
	            }
	        )    
	    }
	    
	};
    
    return {
        
        PressSearchButton: function()
        {
            priv.inputSubmit();
        },

        /**
		 * should be triggered on $(document).ready
		 */
		OnReady	: function(){
		    //var cookieStr = $.cookie(logic.CookiePrefix + "enquete");
		    //if(!document.referrer && (!cookieStr || cookieStr.length == 0)){
		    //    location.href = resources.path_prefix + '/prijsvraag.aspx';
		    //}
					
			//priv.bindPromoBlockEvents();
			//priv.bindCountryLocations();
			
//			$('#homepagetopleft').bind("click",
//                function(){                   
//                    document.location.href = resources.path_prefix + '/search.aspx';
//                    return false;
//                }
//            );
			
//			$('#country-box').bind("click",
//                function(){
//                    //redirect to the correct page
//                    var url = $(this).find("a.countryurl").attr("href");
//                    
//                    document.location.href = url;
//                    //prevent event bubbling
//                    return false;
//                }
//            );
//            if($('#country-box .hidden').text().indexOf('.jpg') != -1){
//                $('#randombox-content').css('background','url('+$('#country-box .hidden').text()+') right top no-repeat');
//            }
//            
//            
//            priv.writeFlashBanner();
             
            $('#btn-search').bind("click",
                function(){               
                   priv.inputSubmit(this);
                }
            ).bind("change", 
                function(){
                    priv.allowInputSubmit = true;
                }
            );
            $('#txtsearchbox').bind("click",
                function(){
                    this.value = '';
                }
            ).bind("keyup",
                function(evt){
                    if(evt.keyCode == 13){
                        priv.inputSubmit();
                    }
                }
            );

            $("ul.promos li").bind('click', function(){
                location.href = $(this).find('a').attr('href');
            });
		}
        
    };
}(jQuery);
