/* NAME : GLOBAL JS FILE
   DESCRIPTION: Define most common JS Function
   Created For: ADC
 */

$(document).ready(function(){
if(document.uniqueID && !window.XMLHttpRequest ){document.execCommand("BackgroundImageCache",false,true);}
    //For Fatwire Tables
    $('.tableContainer').find('tbody tr:even').addClass('even');
    $('.tableContainer').find('tbody tr:first-child').addClass('tableHeader');
    $('.tableContainer').find('tbody tr.tableHeader td:first-child').addClass('topLeftCorner');
    $('.tableContainer').find('tbody tr.tableHeader td:last-child').addClass('topRightCorner');
    $('.tableContainer').find('tr td:first-child').addClass('leftBorder');
    $('.tableContainer').find('tr td:last-child').addClass('rightBorder');
    
    //For Alternate Rows Colour
    $('.alternateRowsTable').find('tbody tr:even').addClass('even');
    $('table.oddEvenTable').find('tbody tr:even').addClass('even');
    $('table.oddEvenTable td.nestedTable table').find('tbody tr').removeClass('even');
    LoginRedirect();   
    slideUpDown();
    if(jQuery('.custom-tabs ul li').length){customTabs();}
    if(jQuery('.custom-tabs .nestedtabs').length){nestedTabs();}
    toggleMode();
    viewmorePosition();
    hidepipeSign();
	if(jQuery('.imageGallery-threeImage img').length) jQuery('.imageGallery-threeImage img:last-child').addClass('last');
	if(jQuery('.tabs-content .tabPanel').length) jQuery('.tabs-content .tabPanel').eq(0).css('display','block');
	if(jQuery('a.viewDrawing').length){ jQuery('a.viewDrawing').bind('click', function(e){viewmoreLink(e)})};
});

// Funtion for Popup Window [START]
    var win = null;
  function newWindow(mypage,myname,w,h,scroll,slideId){
        LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
        TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
        var urlStr = mypage+"&slideId="+slideId;
        //var urlStr = mypage+"?"+slideId;
        settings =
        'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable'
        win = window.open(urlStr,myname,settings)
  }
// Funtion for Popup Window [END]

// Funtion for load PDF functionality [START]
  function loadPdf(url){
      viewWindow = window.open(url, "ViewContent", "width=700,height=480,scrollbars=yes,toolbar=no,location=0,status=yes,menubar=no,resizable=yes,directories=no");
      viewWindow.focus();
  }
// Funtion for load PDF functionality [END]

// Funtion for login [START]
  function LoginRedirect(){
    $("li.login a.thickbox").attr("href", "#TB_inline?height=330&amp;width=380&amp;inlineId=loginLayer&amp;modal=true");
  }
// Funtion for login [START]

//Function for slide up and down for request type on contact us page [START]
  function slideUpDown(){
      $("#requestType").change(function(){
          if ($(this).val() == "Technical Support" ) {
                $("li.reqstTypeDetails").slideDown("fast");
                $("li.hiddenField input").addClass('required');
                $("#subject").addClass('required');
          } 
          else {
               $("li.reqstTypeDetails").slideUp("fast");
               $("li.hiddenField input").removeClass('required');
               $("li.hiddenField input").removeClass('error');
               $("#subject").removeClass('required');
               $("li.hiddenField label").hide();
         }
            }); 
  }
//Function for slide up and down for request type on contact us page [END]

 //Function for nested tabs [START]
  customTabs = function(){
      jQuery('.custom-tabs > ul li a').bind("click",function(e){
          e.preventDefault();
          qIndex = $('.custom-tabs > ul li a').index($(this));
          jQuery('.custom-tabs > ul li a').removeClass();
          jQuery(this).addClass('selected');
          jQuery('.custom-tabs .tabs-content div.tabPanel').hide();
          jQuery('.custom-tabs .tabs-content div.tabPanel').eq(qIndex).show();
      });
  }
  nestedTabs = function(){
      jQuery('.nestedtabs ul li a').bind("click",function(e){
          e.preventDefault();
            qIndex = $('.nestedtabs ul li a').index($(this));
            jQuery('.nestedtabs ul li a').removeClass();
            jQuery(this).addClass('selected');
            jQuery('.nestedtabs .nestedtabs-content div.nestedtabsPanel').hide();
            jQuery('.nestedtabs .nestedtabs-content div.nestedtabsPanel').eq(qIndex).show();
     });
  }
 //Function for nested tabs [END]

 //Function for Expand and Collapse [START]
  toggleMode = function(){
       $("h4.expand a").toggle(function() {
            $(this).parent('h4').removeClass('expand').addClass("collapse"); 
            $(this).parent().next("div.collapse").show();
          },
        function(){
           $(this).parent('h4').removeClass('collapse').addClass("expand");
            $(this).parent().next("div.collapse").hide();
        });
 }
 //Function for Expand and Collapse [END]

 //Function for Position of ViewMore links [START]
  viewmorePosition = function(){
        if((jQuery('.thumbnail-324 a img').length)>1){
            jQuery('.thumbnailContainer span.viewMore').removeClass('thumbnail1').addClass("thumbnail2");
        }
        else{
            jQuery('.thumbnailContainer span.viewMore').removeClass('thumbnail2').addClass("thumbnail1");
        }
 }
 //Function for Position of ViewMore links [END]

 //Function for viewMore links [START]
 viewmoreLink = function(e){
	jQuery(e.target).parents('.tabs-content').find('.tabPanel').css('display','none');
	jQuery(e.target).parents('.custom-tabs').find('.tabs a').removeClass('selected');
	jQuery(e.target).parents('.tabs-content').find('.tabPanel').eq(3).css('display','block');
	jQuery(e.target).parents('.custom-tabs').find('.tabs a').eq(3).addClass('selected');
		
 }
 //Function for viewMore links [END]
 
 //Function for Position of Top Navigations links [START]
  hidepipeSign = function(){
    $('#mainNavigation ul li').hover(  
        function() { 
            $(this).prev('li').find('span').addClass("noBackground");
        }, 
        function() {
             $(this).prev('li').find('span').removeClass("noBackground");
        }
    )
 }
 //Function for Position of Top Navigations links [END]

//Function for Contact Preference selection by user choice
		function contactPreference(){
		prevPhoneFieldVal = $('#hiddenPhone').val();
		prevEmailFieldVal = $('#hiddenEmail').val();
		$("#phoneField").attr("disabled","disabled");
		$("#emailField").val(prevEmailFieldVal);
		$("#phoneField").val('');
		$("li.contactRadio input").removeAttr("checked")
		$("li.contactRadio input").eq(0).attr("checked",'checked')
		$("li.contactRadio input").click(function(){
			var currentAttrID = $(this).attr("id");
			if(currentAttrID == "prefEmail"){
				prevPhoneFieldVal = $("#phoneField").val();
				$("#phoneField").val("");
				$("#phoneField").attr("disabled","disabled");
				if(($("#emailField").attr('disabled'))){
				$("#emailField").removeAttr("disabled");
				$("#emailField").val(prevEmailFieldVal);
				}
				
			}
			if(currentAttrID == "prefPhone"){
				prevEmailFieldVal = $("#emailField").val();
				$("#emailField").val("");
				$("#emailField").attr("disabled","disabled");
				if(($("#phoneField").attr('disabled'))){				
				$("#phoneField").removeAttr("disabled");
				$("#phoneField").val(prevPhoneFieldVal);
				}
				
			}
			if(currentAttrID == "prefBoth"){
				if(!($("#emailField").attr('disabled')))
				prevEmailFieldVal = $("#emailField").val();
				else{
				$("#emailField").removeAttr("disabled");
				
				}
				$("#emailField").val(prevEmailFieldVal);
				
				if(!($("#phoneField").attr('disabled')))
				prevPhoneFieldVal = $("#phoneField").val();
				else{
				$("#phoneField").removeAttr("disabled");
				
				}
				$("#phoneField").val(prevPhoneFieldVal);
				
			}
		});	
			
		}
/* Contact us */
function prodList() { /* function to change text box text on list element click */
         $("li.dropArrow ul.secondLevelClick li a").bind('click', function(e){
			  e.preventDefault();
		 	  e.stopPropagation();
			  var selectedText = $(this).text();
			  var parentSelectedText =$(this).parents("ul.secondLevelClick").parents('li').find('a').eq(0).text();
			  $("li.dropArrow a.first").text(" ");
			  $("li.dropArrow a.first").text(parentSelectedText + "-" + selectedText);
			  $("#productListDropdownValue").val($('#dropBox a.first').text());
			
		 });
		 $("li.dropArrow ul.firstLevelClick > li").bind('click', function(e){
		     e.preventDefault();
			 var selectedText = $(this).find('a').eq(0).text();
		     $("li.dropArrow a.first").text(" ");
	    	 $("li.dropArrow a.first").text(selectedText);
			 $("#productListDropdownValue").val($('#dropBox a.first').text());
			 
		 });
    }
initializeNav = function(){
    jQuery('#toplevel li').hover(  
        function() { 
			$(this).find('ul').eq(0).css('display','block');
			jQuery('input.hide').css('visibility','hidden');
			jQuery('textarea.hide').css('visibility','hidden');
			jQuery('.hide').css('z-index','-1');
			   }, 
        function() {
			$(this).find('ul').eq(0).css('display','none');
        	jQuery('input.hide').css('visibility','visible');
			jQuery('textarea.hide').css('visibility','visible');
			jQuery('.hide').css('z-index','0');
			//jQuery('#dropBox > ul li.dropArrow > ul').css('left','-9999px')
        }
    )
}
/* Contact us */
tabSelection= function(){
		gettabString=window.location.href.split("#"); 
		if(jQuery('#'+gettabString[1]).length){
			jQuery('.tabs-content').find('.tabPanel').css('display','none');
			jQuery('#'+gettabString[1]).css('display','block');
			getIndex=$('.tabs-content div').index(jQuery('#'+gettabString[1]))
			jQuery('.custom-tabs').find('.tabs a').removeClass('selected');
			jQuery('.custom-tabs').find('.tabs a').eq(getIndex).addClass('selected');
		}
}
