String.prototype.trim = function() {
  return (this.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, ""));
}
var $j = jQuery.noConflict();

var mark_menu_opt  = function(nL1,nL2){
        switch(nL1){
            case 0:
                if(nL2==5){
                    $j("#collectionlabelLI").css("color","#FF0099");
                }
                else{
                    $j("li#menucolecciones"+nL2).find("a").css("color","#FF0099");
                }
            break;
            
            case 4:
            case 5:
            case 6:
            case 7:
                $j("#menubot a").eq(nL1-4).css("color","#FF0099");
            break;
            default:
                $j("#accordionmenu>div.menuitem").eq(nL1).find("div.menuitem-content").find("ul").find("li").eq(nL2).find("a").css("color","#FF0099");       
            break;
            
        }
    };     
               
$j(document).ready(function(){
	
	/*
    $j("#accordionmenu").accordion({
        header:"a.menuitem-header",
        autoHeight:false,
        alwaysOpen:false,
		active:4
    });
    $j("#accordionmenu").show();
    */
    
    /*
    var nCover = 0;
    $j("#homeIMG").click(function(){
        $j(this).fadeOut("slow",function(){
            nCover++;
            nCover  = (nCover%4);
            $j(this).attr("src","img/cover0"+nCover+".jpg");            
        });
    });
    */
    
    var nCover = 0;
    var rotateCoverImage = function(){
        $j("#homeIMG").fadeOut("slow",function(){
            nCover++;
            nCover  = (nCover%4);
            $j("#homeIMG").attr("src","img/cover0"+nCover+".jpg");            
        });
    };
    
    $j("#homeIMG").click(function(){
        var aIdLooks    = new Array(21,22,23,24);
        document.location   = "renderGallery.php?pIdGallery="+ aIdLooks[nCover] +"&pIdCollection=1&pMenuOptL1=0&pMenuOptL2=0";
    });
    
    
    window.setInterval(rotateCoverImage,3000);
    
    $j("img.fading").load(function(){
       $j(this).fadeIn("slow");
    }); 
    
    // Gallery
    $j(".gallerythumb").click(function(){
        var nIdImage  = $j(this).attr("nIdImage");
        $j("#foto img, #foto2 img").fadeOut("slow",function(){
            $j(this).attr("src","ognShowImage.php?pId="+ nIdImage);
        });
    });
    
    $j("#foto img, #foto2 img").load(function(){
        $j(this).fadeIn("slow");
    });
    
    // Gallery :: Scroll
    var oScroll = $j("div.scrollable").scrollable({vertical:true, 
                                                  size: 4});
    
    // ScrollPane
    $j(".scroll-pane").jScrollPane();
    
    // NiceShops
    if($j("select[name=\"pIdCountry\"]").val()!=1){
          $j("select[name=\"pIdProvince\"]").parent().hide();
          $j("select[name=\"pIdProvince\"]").val($j(this).attr("nIdDefaultProvince"));
     }
     
     $j("select[name=\"pIdCountry\"], select[name=\"pIdProvince\"]").change(function(){
       var nIdCountry = $j("select[name=\"pIdCountry\"]").val();
       //var nIdProvince = $j("select[name=\"pIdCountry\"]").attr("nIdDefaultProvince");
       var nIdProvince = $j("select[name=\"pIdProvince\"]").val();
       //Ajax call
       $j.post("ajaxListShops.php",
            {pIdCountry: nIdCountry, pIdProvince: nIdProvince},
            function(data){
                switch(data.nErrno){
                    case 1:
                        $j("#shoplistwrapperDIV").fadeOut("slow",function(){
                          $j(this).text(data.strErrMsg);
                           $j(this).fadeIn("slow");
                        });
                        break;
                    case 0:
                       $j("#shoplistwrapperDIV").fadeOut("slow",function(){
                         $j(this).html("");
                         if(data.aShops.length>0){
                             for(i=0;i<data.aShops.length;i++){
                                $j(this).append("<div id=\"shoptienda\"><a href=\"#\" class=\"shopdetaillink\" nid=\""+ data.aShops[i][0] +"\">"+ data.aShops[i][1] +"</a></div>");
                             }
                             $j("#shoplistwrapperDIV a.shopdetaillink").click(linkShopDetail);
                         }
                         else{
                           $j(this).html(data.strErrMsg);  // No shop available
                         }
                         $j(this).fadeIn("slow");
                       });
                       
                       if(data.nIdCountry==1){
                         $j("select[name=\"pIdProvince\"]").parent().fadeIn("slow");
                       }
                       else{
                          $j("select[name=\"pIdProvince\"]").parent().fadeOut("slow");
                          $j("select[name=\"pIdProvince\"]").val($j(this).attr("nIdDefaultProvince"));
                       }
                       break;
                }                    
        },"json");  
       
     });
     
     var linkShopDetail = function(){
        $j("#fShops input[name=\"pId\"]").val($j(this).attr("nId"));
        $j("#fShops").submit();
     }
     $j("#shoplistwrapperDIV a.shopdetaillink").click(linkShopDetail);
     
     // map
     $j("#shopimgmaptoggle").toggle(function(){
            $j(this).html($j(this).attr("strPicture"));
            $j("#shopimgmapwrapper").html("");
            $j("#map").clone().prependTo("#shopimgmapwrapper").show();
            
        },
        function(){
            $j(this).html($j(this).attr("strMap"));
            $j("#shopimgmapwrapper").html("");
            $j("#shopimg").clone().prependTo("#shopimgmapwrapper").show();
            
     });
     
     /***
     * Newsletter
     */
     
     
     //form validation
     var markFormFields   = function(aFormFields){
         for(var i=0;i<aFormFields.length;i++){
             aFormFields[i].prev().prev().addClass("formerror");
         }
     };
     var pickEmptyFormField   = function(oFormField,aFormFields){
         if(oFormField.val()==""){
             aFormFields.push(oFormField);
         }
     };
     
     $j("#newsletterSUBMIT").click(function(){
        
        var oInput;
        var aEmptyFields  = Array();
        var bError   = false;
        
        //Eliminem errors previs
        $j("#newswrapper label").removeClass("formerror");
        
        // Empty textbox & textareas
        pickEmptyFormField($j("#newswrapper input[name=\"pName\"]"),aEmptyFields);
        pickEmptyFormField($j("#newswrapper input[name=\"pSurname\"]"),aEmptyFields);
        pickEmptyFormField($j("#newswrapper input[name=\"pEmail\"]"),aEmptyFields);
        
        //Valid e-mail
        if(!ognValidateEmail($j("#newswrapper input[name=\"pEmail\"]").val())){
            aEmptyFields.push($j("#newswrapper input[name=\"pEmail\"]"));
        }
        
        //Valid birthday
        var strBirthday = $j("#newswrapper select[name=\"pDay\"]").val() +"/"+ $j("#newswrapper select[name=\"pMonth\"]").val() +"/"+ $j("#newswrapper select[name=\"pYear\"]").val(); 
        
        if(!ognIsDate(strBirthday)){
            aEmptyFields.push($j("#newswrapper select[name=\"pDay\"]"));
        }
        
        if(aEmptyFields.length>0){
            markFormFields(aEmptyFields);
            aEmptyFields[0].focus();
            bError  = true;
        }
        if(!bError){
           var strName      = $j("#newswrapper input[name=\"pName\"]").val();
           var strSurname   = $j("#newswrapper input[name=\"pSurname\"]").val();
           var strEmail     = $j("#newswrapper input[name=\"pEmail\"]").val();
           var strAdr       = $j("#newswrapper input[name=\"pAdr\"]").val();
           var strCP        = $j("#newswrapper input[name=\"pCP\"]").val();
           var strVillage   = $j("#newswrapper input[name=\"pVillage\"]").val();
           var strCountry   = $j("#newswrapper input[name=\"pCountry\"]").val();
           var strGenre     = $j("#newswrapper select[name=\"pGenre\"]").val();
           var strProfession= $j("#newswrapper input[name=\"pProfession\"]").val();
           
           //Ajax call
	       $j.post("ajaxNewsletterSubscription.php",
	            {pName: escape(strName),
	             pSurname : escape(strSurname),
                 pEmail : escape(strEmail),
                 pAdr : escape(strAdr),
                 pCP : escape(strCP),
                 pVillage : escape(strVillage),
                 pCountry : escape(strCountry),
                 pGenre : escape(strGenre),
                 pProfession : escape(strProfession),
                 pBirthday : escape(strBirthday)
	            },
	            function(data){
	               $j("#newsletterSUBMIT").hide();
	               $j("#newsbox").css("border-bottom","0px");
	               $j("#newsbox").fadeOut("slow",function(){
                     $j(this).html("<br/><br/>"+data.strErrMsg);
                      $j(this).fadeIn("slow");
                   });        
	        },"json");  
        }

     });
     
     /**
     * Franchises
     */ 
     
     $j("#loginFranchiseMsgboardDIV").click(function(){
         $j(this).fadeOut();
     });
          
     $j("#loginFranchisesSPAN").click(function(){
        var strUsername = $j("#franmenu2 input[name=\"pUsername\"]").val().trim();
        var strPwd      = $j("#franmenu2 input[name=\"pPwd\"]").val().trim();
        if((strUsername=='')||(strPwd=='')){
            alert("No olvide indicar su nombre de usuario y contraseņa");
            return(false);
        }
        //Ajax call
           $j.post("ajaxLoginFranchises.php",
                {pUsername: escape(strUsername),
                 pPwd : escape(strPwd)
                },
                function(data){
                   switch(data.nErrno){
                     case 0:
                        $j("#loginFranchiseMsgboardDIV").text(data.strErrMsg).fadeIn();
                       document.location    = "descargasf.php";
                     break;
                     
                     default:
                        $j("#loginFranchiseMsgboardDIV").text(data.strErrMsg).fadeIn();
                     break;
                     
                   }        
            },"json");  
        
     });
     
     
     /**
     * Press room
     */ 
     
     $j("#loginPressMsgboardDIV").click(function(){
         $j(this).fadeOut();
     });
          
     $j("#loginPressSPAN").click(function(){
        var strUsername = $j("#franmenu2 input[name=\"pUsername\"]").val();
        var strPwd      = $j("#franmenu2 input[name=\"pPwd\"]").val();
        if((strUsername=='')||(strPwd=='')){
            alert("No olvide indicar su nombre de usuario y contraseņa");
            return(false);
        }
        
        //Ajax call
           $j.post("ajaxLoginPress.php",
                {pUsername: strUsername,
                 pPwd : strPwd
                },
                function(data){
                   switch(data.nErrno){
                     case 0:
                        $j("#loginPressMsgboardDIV").text(data.strErrMsg).fadeIn();
                       document.location    = "pressfiles.php";
                     break;
                     
                     default:
                        $j("#loginPressMsgboardDIV").text(data.strErrMsg).fadeIn();
                     break;
                     
                   }        
            },"json");  
        
     });
     
     /***
     *  Nice News
     */
     
     $j("#newsarchiveSELECT").change(function(){
        document.location   = "nicenewsarchive.php?pDate="+ $j(this).val();
     });
     
     /***
     *  Job Opportunities
     */
    $j("#ofertas div.plegathandler").next("div").hide(); 
    $j("#ofertas div.plegathandler").click(function(){
        $j(this).next("div").slideToggle("slow");
    });
    $j("#ofertas div.plegathandler").next("div").click(function(){
        $j(this).slideUp("slow");
    }); 
    
    /**
    *  Nice Clips
    */
    
    var toggle_label = function(){
      $j(this).find("div").toggle();
    };
    $j("#clipswrapper a").hover(toggle_label,toggle_label);
    
    
    if($j("#clipscroll").height()>280){
        $j("#clipscroll").height(280);
        $j("#clipscroll").addClass("scroll-pane");
    }
    
    $j("#clipgallery div.clippicture").click(function(){
      var nIdImage  = $j(this).attr("idimage");
      $j("#clipwrapper #mainpicture").fadeOut("slow",function(){
        
        $j("#clipgallery div.clippicture").html("");
        
        $j("#clipwrapper #mainpicture").attr("src","ognShowImage.php?pId="+ nIdImage +"&pWidth=310&pHeight=425").fadeIn("slow",function(){
          
          $j("#clipgallery div.clippicture[idimage=\""+ nIdImage +"\"]").html("<div class=\"featured\"></div>");
          
          if($j("#clipgallery div.clippicture[idimage=\""+ nIdImage +"\"]").next("div.clippicture").length==1){
            $j("#clipwrapper img#pagup").show();
          }
          else{
            $j("#clipwrapper img#pagup").hide();
          }
          
          if($j("#clipgallery div.clippicture[idimage=\""+ nIdImage +"\"]").prev("div.clippicture").length==1){
            $j("#clipwrapper img#pagdown").show();
          }
          else{
            $j("#clipwrapper img#pagdown").hide();
          }
          
        });
      
      });
      
    });
    
    $j("#clipwrapper img#pagup").click(function(){
      $j("div.clippicture div.featured").html("").parent().next().click();
    });
    
    $j("#clipwrapper img#pagdown").click(function(){
      $j("div.clippicture div.featured").html("").parent().prev().click();
    });
});

(function($j) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $j.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)