// For debug purposes
function debugLog(str) {
  if(typeof console != 'undefined') {
    console.log(str);
  } else {
    $('#debug-info-box').show();
    $('#debug-info-box').append(str+'<br />');
  }
}

//Get scrolling offsets - see how much the window has been scrolled in each directions
function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

// Scrolls to  element specified 
function scrollToElement(element){
  //debugLog("element: ");
  //debugLog(element);
  var selectedPosX = 0;
  var selectedPosY = -60;
              
  while(element != null){
//    selectedPosX += element.offsetLeft;
    selectedPosY += element.offsetTop;
    element = element.offsetParent;
    //debugLog(selectedPosY);
  }
  
  //Load the X and Y offsets into an array
  var scrollOffsets = getScrollXY();
  //debugLog(scrollOffsets); 
   window.scrollTo(selectedPosX,selectedPosY);
  //Only scroll if the #demo-name is not visible
 /* if (scrollOffsets[1] > selectedPosY) {
     window.scrollTo(selectedPosX,selectedPosY);
  }*/
}

/**
 * This function checks the CSS properties to see if the element is actually
 * visible to the user. 
 */
function isVisible(obj) {
  if (obj == document) return true

  if (!obj) return false
  if (!obj.parentNode) return false
  if (obj.style) {
      if (obj.style.display == 'none') return false
      if (obj.style.visibility == 'hidden') return false
  }

  //Try the computed style in a standard way
  if (window.getComputedStyle) {
      var style = window.getComputedStyle(obj, "")
      if (style.display == 'none') return false
      if (style.visibility == 'hidden') return false
  }

  //Or get the computed style using IE's proprietary way
  var style = obj.currentStyle
  if (style) {
      if (style['display'] == 'none') return false
      if (style['visibility'] == 'hidden') return false
  }

  return isVisible(obj.parentNode)
}

//IE6 png fix
function pngContentFix() {
	// Fix transparent png images
	$('.fix').pngFix();		
	$('#relative_container').pngFix();
}

$(document).ready(function() {
  
	// Runs png fix for IE6
	if ($.browser.msie == true && $.browser.version < 7) {
	pngContentFix();
	}
	
	//Calculate selector-content width
	var width = 0;
  $('.selector-content ul li').each(function() {
		width += $(this).outerWidth( true );
	});
	$('.selector-content').css('width', width);
	
	// Shows screenshot lists rollover text element if li element has selected class
  $("#screenshot-list-mobile li.selected").find("span.rollover-text").css({ display:'block' });
  $("#screenshot-list-desktop li.selected").find("span.rollover-text").css({ display:'block' });
	


	/**
   * Screenshot images (mobile)
	 * Shows selected items rollover text and changes screenshot image in screenshot frame 
   */
	$('#screenshot-list-mobile li').click(function() {
    if( !$(this).hasClass("selected") ) {
			$('#screenshot-list-mobile li span.rollover-text').slideUp(400);
			$('#screenshot-list-mobile li').removeClass('selected');
			
			$(this).addClass("selected");
			$('#screenshot-list-mobile li.selected span.rollover-text').animate({height:'toggle'},'400');
			
			var img_id = $(this).find("a").attr('id');
			$("div.screenshot-frame-mobile img.screenshot-img").css({ visibility:'hidden' });
			$("div.screenshot-frame-mobile img#" + img_id + "-src").css({ visibility:'visible' });
		}
  });
	
	/**
   * Screenshot images (desktop)
	 * Shows selected items rollover text and changes screenshot image in screenshot frame 
   */
	$('#screenshot-list-desktop li').click(function() {
    if( !$(this).hasClass("selected") ) {
			$('#screenshot-list-desktop li span.rollover-text').slideUp(400);
			$('#screenshot-list-desktop li').removeClass('selected');
			
			$(this).addClass("selected");
			$('#screenshot-list-desktop li.selected span.rollover-text').animate({height:'toggle'},'400');
			
			
			var img_id = $(this).find("a").attr('id');
			$("div.screenshot-frame-desktop img.screenshot-img").css({ visibility:'hidden' });
			$("div.screenshot-frame-desktop img#" + img_id + "-src").css({ visibility:'visible' });
		}
	});

  // Sidebar open / close functionality
	$(".sidebar-hdr").click(function(){
    $(this).next().slideToggle(400);
		$(this).parent().toggleClass("close");
    return false;
  });	

	// Applys horizontal jScrollPane to element
	$('.accessories-selector').jScrollHorizontalPane({ showArrows:true });
  
  // Applys jTab to element
  $('.tab-container').tabs();
  $('.download-tab-container').tabs();
	
	$('#maps-3-tab-container').tabs();
	$('#maps-2-tab-container').tabs();
	$('#maps-s40-tab-container').tabs();
	
	// update sIFR
	replace_sIFR_3();
  
  // Modify the footer links, so that the page is reloaded when footer links contain 
  // anchor links to a section inside the currently viewed page
  $('.link-block a').each(function(i) {
    var h = $(this).attr('href');
    if (h.indexOf('#') != -1) {
      var hshort = h.replace(/(\?)|(#).*/,'');
      //only modify the "href" value if the link target has "#" and points to the current page
      if(location.href.indexOf(hshort) != -1) {
        $(this).attr( {href: 'javascript:location.href="'+h+'";window.location.reload();void(0);'} );     
      }
    }
  });
});

//disable this
var phoneProductArray = false;