//Used for the hover div
function mouseX(evt) {
      if (!evt) evt = window.event; 
      if (evt.pageX) return evt.pageX; 
      else if (evt.clientX) return evt.clientX + (document.documentElement.scrollLeft ?  document.documentElement.scrollLeft : document.body.scrollLeft); 
      else return 0;
}

function mouseY(evt) {
      if (!evt) evt = window.event; 
      if (evt.pageY) return evt.pageY; 
      else if (evt.clientY) return evt.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop); 
      else return 0;
}

function followInfo(evt) {
      var offX = 20;       // X offset from mouse position
      var offY = -50;         // Y offset from mouse position      
      
      if (document.getElementById) {
            var obj = document.getElementById('infoDiv').style;                   
            obj.left = (parseInt(mouseX(evt))+offX) + 'px';
            obj.top = (parseInt(mouseY(evt))+offY) + 'px';            
      }
 
}

function show_Div(evt, text, w) {   
      offX = -(w / 2);
      
      document.onmousemove=followInfo;       
      
      if (w == 'auto') { //auto is set for images that are varying widths
            document.getElementById('infoDiv').style.width=w;
      } else {
            document.getElementById('infoDiv').style.width=w+'px';
      }
      
      document.getElementById('infoDiv').style.visibility='visible';
      document.getElementById('infoDiv').innerHTML=text 
}

function hide_Div() {
      document.getElementById('infoDiv').style.visibility='hidden'; 
      document.onmousemove=mouseY;
}

//help icon information on admin pages
function followHelpInfo(evt) {
      var offX = -400;       // X offset from mouse position
      var offY = 25;         // Y offset from mouse position      
      
      if (document.getElementById) {
            var obj = document.getElementById('infoDiv').style;                   
            obj.left = (parseInt(mouseX(evt))+offX) + 'px';
            obj.top = (parseInt(mouseY(evt))+offY) + 'px';            
      }
 
}

function show_help_Div(evt, text, w) {   
      offX = -(w / 2);
      
      document.onmousemove=followHelpInfo;       
      
      if (w == 'auto') { //auto is set for images that are varying widths
            document.getElementById('infoDiv').style.width=w;
      } else {
            document.getElementById('infoDiv').style.width=w+'px';
      }
      
      document.getElementById('infoDiv').style.visibility='visible';
      document.getElementById('infoDiv').innerHTML=text 
}

function hide_help_Div() {
      document.getElementById('infoDiv').style.visibility='hidden'; 
      document.onmousemove=mouseY;
}

//Confirm delete function
function confirmDelete(text) {
    return confirm(text)      
}

//Gets background position as using .css in IE6 to get position doesn't work
function getBackgroundPosition(div) {
    var p = $(div).css('background-position');
    if(typeof(p) === 'undefined') return $(div).css('background-position-x') + ' ' + $(div).css('background-position-y');
    else return p;
};

// Products Tabs
function setUpTabs(tab) {
    $(".tab_content").hide(); //Hide all content
    $("ul.tabs li."+tab).addClass("active").show(); //Activate first tab
    $(".tab_content."+tab).show(); //Show first tab content
    
    $("ul.tabs li").click(function() {
        $("ul.tabs li").removeClass("active"); //Remove any "active" class
    	$(this).addClass("active"); //Add "active" class to selected tab
    	$(".tab_content").hide(); //Hide all tab content
    	
    	//get hash content from the url  
        var activeTab = $(this).find("a").attr("href");
        var pos = activeTab.lastIndexOf('#');
        activeTab = activeTab.substring(pos); 
        activeTab = activeTab+"content"; //Find the rel attribute value to identify the active tab + content
        $(activeTab).fadeIn(0); //Fade in the active content

        $('#categories-nav-holder').css('height', $("#contentDiv").outerHeight()); //resize the #categories-nav-holder to be same height as product-details                         
        return true; 
    });
} 

function createCookie(name,value,secs) {
	if (secs) {
		var date = new Date();
		date.setTime(date.getTime()+(secs*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
} 

//Set up default div to show on slider pages
function showDefaultDiv(div,divRight) {            
    var $showDiv = $("#"+div);  
    
    if (divRight == true) { 
        $showDiv.css("right","0");    
    }else{
        $showDiv.css("left","0");
    }     
    $("#categories-nav-holder").css("height", $showDiv.outerHeight());  
} 

function showProductNew(contentRight,contentRTL,anchor) {                                
    var $contentDiv = $("#contentDiv");
    var $loadingDiv = $("#loadingDiv");
    var callback = function () {};
    var fullwidth = $loadingDiv.outerWidth();
    
    if (anchor != undefined) callback = function() {
        createCookie('animate','true',5);
        window.location = anchor.href;
    }                                                
    
    // animations
    if (contentRight == true && contentRTL == true) {            
        //menu page - loading to menu
        $loadingDiv.animate({left: -fullwidth});  // hide
        $contentDiv.animate({right: 0}); // show        
        
    }else if (contentRight == true && contentRTL == false) {            
        //menu page - menu to loading
        $contentDiv.animate({right: -fullwidth}); // hide
        $loadingDiv.animate({left: 0});  // show
        
    }else if (contentRight == false && contentRTL == true) {    
        //product/category page - content to loading
        $contentDiv.animate({left: -fullwidth}); // hide
        $loadingDiv.animate({right: 0});  // show
        
    }else if (contentRight == false && contentRTL == false) {    
        //product/category page - loading to content
        $loadingDiv.animate({right: -fullwidth});  // hide
        $contentDiv.animate({left: 0}); // show
    }            
    
    $("#categories-nav-holder").animate({
            height: $contentDiv.outerHeight()
        },
        callback
    );            
}

//change subcategory images when hovering over subcategory on menu
function showSubcatImages(groupid, subcategoryname, image1, image2){
     document.getElementById('group'+groupid+'image1').src = image1;
     document.getElementById('group'+groupid+'image1').alt = subcategoryname;
     document.getElementById('group'+groupid+'image2').src = image2;
     document.getElementById('group'+groupid+'image2').alt = subcategoryname;
}

//to ensure correct height is set for categories-nav-holder, prevents incorrect height set when content hasn't finished loading
function checkResizeTimeouts(){
    setTimeout('checkResize();',250);
    setTimeout('checkResize();',500);
    setTimeout('checkResize();',1000);
    setTimeout('checkResize();',2000);
    setTimeout('checkResize();',4000);
    setTimeout('checkResize();',6000);
    setTimeout('checkResize();',8000);
    setTimeout('checkResize();',9000); 
}
var resizedHeight=0;
function checkResize() {
    if ($("#contentDiv").outerHeight() == resizedHeight) return;              
    resizedHeight = $("#contentDiv").outerHeight();            
    //$("#categories-nav-holder").css("height", resizedHeight);
    $("#categories-nav-holder").animate({height: resizedHeight});
}

function startTitleFader() {
    $('#titlefader').innerfade({				
    	speed: 'slow',
    	timeout: 7000,
    	type: 'sequence',
    	containerheight: '1em'
    }); 
}

function startNewsSlider() {
    $("#newsslider").jCarouselLite({
    	vertical:false,
    	hoverPause:true,
    	visible: 4,
    	auto:100, //amount of time between 2 slides.
    	speed:2500 //speed of the sliding speed
    });
}

// when the object gains focus by the user
function handleFocus(element, value, textcolour)  {
     if (element.value == value) {
           element.value = '';
           element.style.color = textcolour;
     }
} 
//when a user loses focus of a form element
function handleBlur(element, value, textcolour) {
      if (element.value == '') {
            element.value = value;
            element.style.color = textcolour;
      }
}

function validateSearch(){
    var string;
    var stringlength;
    
    string = document.getElementById('search').value;
    
    stringlength = string.replace(/\s/g, "");
    stringlength = stringlength.length;
    
    if (stringlength == 0 || string == "Search Roxspur..." ){
        document.getElementById('search').value = "Search Roxspur...";
        document.getElementById('search').style.color = "#888";
        return false;
    } else {
        return true;
    }
}

function id(element) {
   return document.getElementById(element);
} 

//------------------INTRO LANDING PAGE FUNCTIONS-----------------------//

function setDefaultValue(value) {
    id('hovervalue').value = value;
} 

function setOpacity(obj, opacity) {
  // IE/Win 
  ieopacity = opacity*100;
  obj.style.filter = "alpha(opacity:"+ieopacity+")";
  
  obj.style.KHTMLOpacity = opacity; // Safari<1.2, Konqueror
  obj.style.MozOpacity = opacity; // Older Mozilla and Firefox
  obj.style.opacity = opacity; // Safari 1.2, newer Firefox and Mozilla, CSS3  
}                              

// function fadein(objectID){
//     object = id(objectID);
//     setOpacity(object, 0);
// 
//     animatefadein = function () {
//         if(object.style.opacity < 1){ 
//             var current = Number(object.style.opacity); 
//             var newopac = current + Number(0.1);
//             var newopac = newopac.toPrecision(1);                    
//             setOpacity(object, String(newopac)); 
//             setTimeout('animatefadein()', 100);
//         }
//     }
//     animatefadein();
// }

function fadein(objectID){
    object = id(objectID);
    setOpacity(object, 0);

    animatefadein = function () {
        if(object.style.opacity < 1){ 
            var current = Number(object.style.opacity); 
            var newopac = current + Number(0.01);
            //var newopac = newopac.toPrecision(1);                    
            setOpacity(object, String(newopac)); 
            setTimeout('animatefadein()', 1);
        }
    }
    animatefadein();
}
            
function showIntroDiv(element) {
    //HIDE PREVIOUS HOVER DIV
    var lastelement;
    lastelement = id('hovervalue').value;

    if (lastelement == "instrumentationintro"){
        id(lastelement).style.backgroundPosition = '-300px -800px';
    } else if (lastelement == "calibrationintro"){
        id(lastelement).style.backgroundPosition = '-600px -800px';
    } else if (lastelement == "measurementintro"){
        id(lastelement).style.backgroundPosition = '-900px -800px'; 
    }
    
    if (lastelement == "mainintro"){
        id('mainintrotext').style.display = 'none'; //roxspur logo bar
        id(lastelement).style.backgroundPosition = '0px -800px'; 
    } else {
        setOpacity(id(lastelement), 1); //set opacity of last div 
        id(lastelement+'-links').style.display = 'none'; //hide links on divs - none on mainintro
        id(lastelement+'-title').style.color = '#666666'; //title   
    }            
    
    //SHOW NEW HOVER DIV
    if (lastelement != element) { fadein(element);}
    
    //background image
    if (element == "instrumentationintro"){
        id(element).style.backgroundPosition = '-300px 0px';
    } else if (element == "calibrationintro"){
        id(element).style.backgroundPosition = '-600px 0px';    
    } else if (element == "measurementintro"){
        id(element).style.backgroundPosition = '-900px 0px'; 
    }
    
    if (element == "mainintro"){
        id('mainintrotext').style.display = 'block'; //roxspur logo bar
        id(element).style.backgroundPosition = '0px 0px'; 
    } else {
        id(element+'-links').style.display = 'block'; //hide links on divs - none on mainintro
        id(element+'-title').style.color = '#ffffff'; //title    
    } 
   
    id('hovervalue').value = element;            
}

function directToPage(groupid) {
    createCookie('menuGroup',groupid);
}
