﻿var maxWidth;      
var primHeight, secHeight;

//$(window).resize(function(){            
//    primHeight=$("#primary-wrapper").height();
//    secHeight=$("#secondary-wrapper").height();
//   
//    if (primHeight > secHeight) {
//        $("#secondary-wrapper").height(primHeight);
//    } else {
//        $("#primary-wrapper").height(secHeight);
//    }                  
    
    //var prim = $("#primary-wrapper").width() + 30;
    //alert(prim);
    //$("#myImage").dialog().css("top", 50);
//    if ($.browser.msie) {
//       maxWidth=document.documentElement.scrollWidth - 1;               
//    } else {
//       maxWidth=Math.max($("html").width(), $(window).width() - 1);               
//    }                       
    
    //$("#cell-related-items").width(maxWidth - 670);
    //$("#content").width(maxWidth);
    //$("#secondary-wrapper").width(maxWidth - prim); 
//});    
        
$(function(){      
    try{                        
        $.each($("img.thumbtint, input.thumbtint"), function(i,obj){           
            $(obj).hover(function(){		
                $(obj).fadeTo("fast", 0.5); 
            },function(){
                $(obj).fadeTo("fast", 1.0);// This sets the opacity to 100% on off hover	
            });
        });              
        
        $("#hyPLC").hover(function(){	             
            $("#imgFakeRollOver").removeClass("hide");                  
        },function(){
            $("#imgFakeRollOver").addClass("hide");  
        });     

        $.each($("a"), function(i,obj){
            if ($(obj).attr("rel") == "lytebox"){       
                var guid = (((1+Math.random())*0x10000)|0).toString(16).substring(1);
                imgObj = new Image();
                imgObj.src=$(obj).attr("href");
                imgObj.id=guid;                    
                $("body").append($(imgObj).addClass("hide"));
                                     
                $(obj).click(function() {
                    $("#myImageSrc").attr("src",$(obj).attr("href"));
                    $("#myImageSrc").attr("alt",$(obj).attr("title"));
                    $("#video").hide();
                    $("#myImageSrc").fadeIn();
                    
                    var myImgObj = $("#" + guid);
                    $("#myImage").dialog({   
                          height: $(myImgObj).height() + 75,
                          width: $(myImgObj).width() + 45,                    
                          modal: true, 
                          title: $(obj).attr("title"),
                          overlay: { 
                              opacity: 0.5, 
                              background: "black" 
                        } 
                    }).show();
                    
                    $(".ui-dialog-overlay").click(function(){
                        $("#myImage").dialog("close");                     
                    });
                                                                                                                                                                                                                                                                                                                                                     
                    return false;             
                });
            }            
        });    
        
        $.each($("a"), function(i,obj){
            if ($(obj).attr("rel") == "pop-up-video"){               
                                                       
                $(obj).click(function() {                                        
                    $("#myImageSrc").hide();     
                    $("#video").fadeIn();                               
                    
                    $("#myImage").dialog({   
                          height: 480,
                          width: 480,                    
                          modal: true, 
                          title: $(obj).attr("title"),
                          overlay: { 
                              opacity: 0.5, 
                              background: "black" 
                        } 
                    }).show();
                                      
                    var approot=$("#hidAppRoot").val();
                    
                    var objHref=$(obj).attr("href").replace("../","");
                    objHref = objHref.substring(objHref.lastIndexOf("/") + 1, objHref.length);
                    
                    var playerurl = approot + objHref; 
                    $("#video").text("...One Moment Please...");                   
                    
                    var p=getQStringVars(playerurl, "player");
                    var v=getQStringVars(playerurl, "video");
                    var h=getQStringVars(playerurl, "height");
                    var w=getQStringVars(playerurl, "width");
                    //alert("p: " + p + "  v: " + v + "  h: " + h + "  w: " + w);
                    $("#video").load(playerurl,
                        {
                            Player: p,                     
                            Video: v, 
                            Height: h,                  
                            Width: w,
                            StreamIt: true                        
                        },
                        function (result,type) {   
                            //alert($.trim(result)); 
                            $(this).html(result);                                                       
                        } 
                    );        
                    
                    $(".ui-dialog-titlebar-close").click(function(){                        
                        $("#video").html("");  
                    });
                    
                    $(".ui-dialog-overlay").click(function(){
                        $("#myImage").dialog("close");
                        $("#video").html("");  
                    });
                                                                                                                                                                                                                                                                                                                                                                  
                    return false;             
                });
            }            
        });                               
            
        //$.preloadCssImages();        
        $(".imgover").imageOver();      
           
        var root=$("#hidAppRoot").val();    
        $("#menu-ul").setCurrentSection(root);
        $("#menu-ul").fadeIn("slow");
        
//        var pageimgages="";
//        $.each($("#primary-content img"),
//            function(i,obj){        
//                pageimgages += $(obj).attr("src") + ",";                                                             
//            }                         
//        );              
//           
//        if ($.trim(pageimgages).length > 0){    
//            $.preloadImages(pageimgages.substring(pageimgages.length-1));        
//        }         
    
    }catch(e){        
        //alert("Error:" + e);       
    }                           
});

function getQStringVars(qStr,variable){ 
    var query = qStr;    
    var query = query.substring(query.lastIndexOf("?") + 1, query.length);
    var vars = query.split("&"); 
  
    for (var i=0;i<vars.length;i++) { 
        var pair = vars[i].split("="); 
        if (pair[0] == variable) { 
            return pair[1]; 
        } 
    } 
    //else not found
    return 0; 
} 
            
$.fn.setCurrentSection = function(appwebroot){    
     try{  
        var currenturl = "";             
        var currenthost = window.location.href.replace('http://','');     
	    currenthost = currenthost.replace(currenthost.substring(0,currenthost.indexOf('/')),'');
	
	    var filename = location.pathname.substring(location.pathname.lastIndexOf('/')+1) 	
        currenthost = currenthost.replace(filename,'');
        currenthost = currenthost.replace(appwebroot,'');             
     
        var dir_array=currenthost.split("/");
                  
        if (dir_array.length > 0){           
            currenthost = dir_array[0];                                                             
            currenturl = currenthost.toLowerCase().replace(/\//g,'');	    
    	     	    
            if ($.trim(currenturl).length > 0 && currenturl.indexOf('.') <= 0){                              
                var section=currenturl;
                var objSrc=$("." + section).attr("src");        
                var ftype = objSrc.substring(objSrc.lastIndexOf("."), objSrc.length);     
                var objSrcOver = objSrc.replace(ftype, "-over" + ftype);             
                var objSrcActive = objSrc.replace(ftype, "-active" + ftype);       
                        
                $("." + section).attr("src",objSrcActive);
                var parentObj=$("." + section).parent();                
                
                (parentObj).hover(function(){	                                     
                        $(parentObj).find("img").attr("src",objSrcOver);                    	            
                    },function(){                       
                        $(parentObj).find("img").attr("src",objSrcActive);
                });  
                
                var MySub = $(parentObj).siblings("ul");              
                
                $(MySub).hover(function(){	              
                        $(parentObj).find("img").attr("src",objSrcOver);  
                    },function(){                               
                        $(parentObj).find("img").attr("src",objSrcActive);
                });           
            }    
        }
        
     }catch(e){        
        //alert("Error:" + e);       
    }               
}