jQuery.noConflict();
jQuery(document).ready(function(){



    jQuery.fn.heightCenter = function () {

        if(jQuery.browser.msie){
            this.css("top", ( (jQuery(window).height() - 170) / 2 ) + "px");
        }else{
            this.css("top", ( (jQuery(window).height() - 170) / 2 ) + "px");
        }
        
        return this;
    }

    jQuery(window).resize(function() {
        jQuery('#productsWrapper').css('height',(jQuery(document).height())+'px');
        jQuery('#productsWrapper').css('max-height',(jQuery(document).height())+'px');
//        jQuery(function(){
//            if(jQuery('#productsWrapper').data('jsp') != null){
//                jQuery('#productsWrapper').data('jsp').reinitialise();
//            }
//        });
    });
    
    

    //jQuery(window).load(function(){
        var browser = navigator.userAgent.toLowerCase().indexOf('chrome');

        jQuery('#ajaxRenderWrapper').width((jQuery(window).width()-340)+'px');
        jQuery('#ajaxRenderWrapper').height(jQuery(window).height()+'px');
        //alert(jQuery('#ajaxRenderWrapper').width());
        jQuery('#weatherContainer').width(jQuery(window).width()+'px');
        jQuery('#weatherContainer').height(jQuery(window).height()+'px');
        if(browser > -1){//chrome problem height
            if(jQuery(document).height() > jQuery(window).height()){
                jQuery('#weatherContainer').height(jQuery(window).height()+17+'px');
            }
        }

        jQuery('#leftMiddleWrapper').heightCenter();

        jQuery('#rightMiddleWrapper').heightCenter();
    //});
    

    jQuery('.mainMenu > img').mouseenter(function() {
       tempMainMenuImgSrc = jQuery(this).attr('src');
       var tempArray = tempMainMenuImgSrc.split(".");
       var ext = tempArray[tempArray.length-1];
       var tempSrcWithoutExt = tempMainMenuImgSrc.replace("."+ext, "");
       if(tempSrcWithoutExt.indexOf("Hover") == -1){
            jQuery(this).attr('src',tempSrcWithoutExt+'Hover.'+ext);
       }
      });

      jQuery('.mainMenu > img').mouseout(function() {
       tempMainMenuImgSrc = jQuery(this).attr('src');
       if(!jQuery(this).hasClass('activeMainMenu')){
           if(tempMainMenuImgSrc.indexOf('Hover') >= 0){
                var tempArray = tempMainMenuImgSrc.split("Hover");
                jQuery(this).attr('src',tempArray[0]+tempArray[1]);
            }
       }
      });

      

      //main menu
      //logo
      jQuery('#rightTopWrapper').click(function() {
          currentLang = jQuery('.currentLang').attr('id');
          window.location="http://technolen.eu/"+currentLang;
      });
      //logo

      //produkty
      jQuery('.productsMenuImage').click(function() {
          productsMenuMainClicked(jQuery(this),false);
           
       });
       //produkty

       //spolecnost
       jQuery('.companyMenuImage').click(function() {
           companyMenuMainClicked(jQuery(this));
          
       });
       //spolecnost

       //kontakt
      jQuery('.contactMenuImage').click(function() {
        contactMenuMainClicked(jQuery(this));
       });
       //kontakt

       //news
      jQuery('.newsMenuImage').click(function() {
        newsMenuMainClicked(jQuery(this),false);
       });
       //news

      //main menu event




});

function hoverMenuClicked(element){
          //set normal for others
          jQuery('.mainMenu > img').each(function(){
            tempAttrSrc = jQuery(this).attr('src');
            if(tempAttrSrc.indexOf('Hover') >= 0){
                var tempArray = tempAttrSrc.split("Hover");
                jQuery(this).attr('src',tempArray[0]+tempArray[1]);
                jQuery(this).removeClass('activeMainMenu');
            }
          });
          //set normal for others
          var tempArray;
          tempMainMenuImgSrc = element.attr('src');
          if(tempMainMenuImgSrc.indexOf('Hover') == -1){
                tempArray = tempMainMenuImgSrc.split(".");
                var ext = tempArray[tempArray.length-1];
                var tempSrcWithoutExt = tempMainMenuImgSrc.replace("."+ext, "");
                element.attr('src',tempSrcWithoutExt+'Hover.'+ext);
           }else{
                element.attr('src',tempMainMenuImgSrc);
           }
          element.addClass('activeMainMenu');
}

function productsMenuMainClicked(element,isOnInit){
           hoverMenuClicked(element);
           tempMainMenuImg = element;
           tempMainMenuImgClass = tempMainMenuImg.attr('alt');

           currentLang = jQuery('.currentLang').attr('id');
           jQuery.ajax({
                    type: "POST",
                    url: "http://technolen.eu/index.php/"+currentLang+"/catalog/",
                    data: "action=products&lang="+currentLang+"&root="+tempMainMenuImgClass+"&title="+tempMainMenuImgClass+"&cat_id=&product_id=",
                    success: function(data){
                        jQuery('#ajaxRenderWrapper').html(data);
                        var minWidth = 200;
                        var tempWidth = jQuery('#productsMenuChildren').width();
                        jQuery('.productsMenuChild strong').each(function (){
                            if(minWidth > tempWidth){
                                tempWidth = minWidth - 10;
                            }else{
                                tempWidth = tempWidth;
                            }
                        });
                        jQuery('#productsMenuChildren').css('width',tempWidth + 'px');
                        jQuery('#currentFirmProducts,#product').hide();


                        },
                        error: function(xmlhttp, status, error){
                            //alert(error);
                        },
                        beforeSend: function(){
                        },
                        complete: function(){
                            if(isOnInit && (initHash.indexOf('#cat') != -1 || initHash.indexOf('#item') != -1)){
                                if(initHash.indexOf('#cat') != -1){
                                    var catElement = jQuery(initHash);
                                    //alert(catElement.attr('id')+' idecko');
                                    productsMenuChildClicked(catElement,false);
                                }else if(initHash.indexOf('#item') != -1){
                                    var tempArray = initHash.split("_catID-");
                                    var initCatId = tempArray[1];
                                    catElement = jQuery('.productsMenuChild[href*="#cat-'+initCatId+'"]');
                                    //alert(catElement.attr('id')+' idecko');
                                    productsMenuChildClicked(catElement,true);
                                }

                            }
                        }
                  });

}

function companyMenuMainClicked(element){
    hoverMenuClicked(element);

           tempMainMenuImg = element;
           tempMainMenuImgClass = tempMainMenuImg.attr('alt');

           currentLang = jQuery('.currentLang').attr('id');
           jQuery.ajax({
                    type: "POST",
                    url: "http://technolen.eu/index.php/"+currentLang+"/catalog/",
                    data: "action=company&lang="+currentLang+"&root="+tempMainMenuImgClass+"&title="+tempMainMenuImgClass+"&cat_id=&product_id=",
                    success: function(data){
                            jQuery('#ajaxRenderWrapper').html(data);
                            if(jQuery('#companyMenuChildren').is(':visible')){
                                var minWidth = 220;
                                var tempWidth = jQuery('#companyMenuChildren').width();
                                jQuery('.companyMenuChild strong').each(function (){
                                    if(minWidth > tempWidth){
                                        tempWidth = minWidth - 40;
                                    }else{
                                        tempWidth = tempWidth;
                                    }
                                });
                                jQuery('#companyMenuChildren').css('width',tempWidth + 'px');
                                jQuery('#companyCurrentDetail').css('left',(40+tempWidth) + 'px');
                            }

                        },
                        error: function(xmlhttp, status, error){
                            //alert(error);
                        },
                        beforeSend: function(){
                        },
                        complete: function(){
                            if(jQuery('#companyMenuChildren > a').length > 1){
                                jQuery('#companyMenuChildren > a').first().click();
                            }
                        }
                  });
}

function newsMenuMainClicked(element,isOnInit){
           hoverMenuClicked(element);

           tempMainMenuImg = element;
           tempMainMenuImgClass = tempMainMenuImg.attr('alt');

           currentLang = jQuery('.currentLang').attr('id');
           jQuery.ajax({
                    type: "POST",
                    url: "http://technolen.eu/index.php/"+currentLang+"/catalog/",
                    data: "action=news&lang="+currentLang+"&root="+tempMainMenuImgClass+"&title="+tempMainMenuImgClass+"&cat_id=&product_id=",
                    success: function(data){
                            jQuery('#ajaxRenderWrapper').html(data);
                            //jQuery('#news').live('load', function() {
                            jQuery('#news').css('width',jQuery('#searchWrapper').width()+'px');
                            jQuery('#news').css('top',jQuery('#rightMiddleWrapper').offset().top+'px');//jQuery('#rightMiddleWrapper').css('top')+
                            var minHeight = jQuery(window).height() - jQuery('#news').position().top;
                            jQuery('#news').css('min-height', minHeight + 'px');

                            appendNewsPager(1);
                            //});
                        },
                        error: function(xmlhttp, status, error){
                            //alert(error);
                        },
                        beforeSend: function(){
                        },
                        complete: function(){
                            if(isOnInit){
                                var tempArray = initHash.split("-");
                                if(tempArray.length > 1){
                                   newsElement = jQuery(initHash);
                                   //alert(initHash);
                                   newsItemClicked(newsElement);
                                }
                            }
                        }
                  });
}

function contactMenuMainClicked(element){
    hoverMenuClicked(element);

           tempMainMenuImg = element;
           tempMainMenuImgClass = tempMainMenuImg.attr('alt');

           currentLang = jQuery('.currentLang').attr('id');
           jQuery.ajax({
                    type: "POST",
                    url: "http://technolen.eu/index.php/"+currentLang+"/catalog/",
                    data: "action=contact&lang="+currentLang+"&root="+tempMainMenuImgClass+"&title="+tempMainMenuImgClass+"&cat_id=&product_id=",
                    success: function(data){
                            jQuery('#ajaxRenderWrapper').html(data);
                            if(jQuery('#contactMenuChildren').is(':visible')){
                                var minWidth = 200;
                                var tempWidth = jQuery('#contactMenuChildren').width();
                                jQuery('.contactMenuChild strong').each(function (){
                                    if(minWidth > tempWidth){
                                        tempWidth = minWidth - 10;
                                    }else{
                                        tempWidth = tempWidth;
                                    }
                                });
                                jQuery('#contactMenuChildren').css('width',tempWidth + 'px');
                                jQuery('#contactCurrentDetail').css('right',(30+tempWidth) + 'px');
                            }
                        },
                        error: function(xmlhttp, status, error){
                            //alert(error);
                        },
                        beforeSend: function(){
                        },
                        complete: function(){
                        }
                  });
}
