// vars
     set_count = 9;
     SearchRequestUID = '';


var currentWidth = 0;
function updateLayout() {
    var currentWindowWidth = window.innerWidth!=undefined?window.innerWidth:document.body.clientWidth;
   if (currentWindowWidth != currentWidth) {
	  currentWidth = currentWindowWidth;
	  var orient = currentWidth == 320 ? "profile" : "landscape";
	  document.body.setAttribute("orient", orient);
	  document.body.setAttribute("id", orient);
	  setTimeout(function() { window.scrollTo(0, 1); }, 100);            
	  if( $('#content').css('left') != '0px' && $('#content').css('left') != '-' + currentWidth + 'px' ) {
	  $('#content').css('left', '-' + currentWidth + 'px' );
	  }
   }
}

$(document).ready(function(){
	updateLayout();
	default_search_label = $('#keywords').val();
	$('#keywords').focus(function(){
		if( $('#keywords').val() == default_search_label ) {
			$('#keywords').val('');
			$('#keywords').css( { color:'#000000' });
		}
	} );
	$('#keywords').blur(function(){
		if( $('#keywords').val() == '' ) {
			$('#keywords').css( { color:'#aaaaaa' } );
			$('#keywords').val( default_search_label );
		}
          else search( 1, '' );
	} );
	$("#search_tool").submit(function(){
          $('#keywords').blur();
          return false;
	} );
     $("#regions").change(function(){
          region_switch( $(this).val() );
     } );
     $('#region_trigger').click(function(){
          toggle_regions('regions');
     } );
     $('#options_trigger').click(function(){
          toggle_options( 'options_1' );
     } );
});

function search( startPosition, SearchRequestUID ) {
	SearchRequestUID = $('#SearchRequestUID').val();
	$('#results').load( 
          "search.php", {
               keywords: $('#keywords').val(),
               SearchRequestUID: $('#SearchRequestUID').val(),
               startPosition: startPosition,
               region: $('#regions').val()
          }, 
          function() {
               // prev set
                    $( '#btn_prev' ).unbind( 'click' );
                    if( startPosition > set_count ) {
                         $('#btn_prev').css('visibility','visible');
                         $("#btn_prev").click(function(){
                              search( ( startPosition - set_count ), SearchRequestUID );
                         } );
                    }
                    else $('#btn_prev').css('visibility','hidden');
               // next set
                    $('#btn_next').unbind('click');
                    $('#btn_next').css('visibility','visible');
                    $('#btn_next').click(function(){
                         search( ( startPosition + set_count ), SearchRequestUID );
                    } );
               window.scrollTo(0,1);
          }
     );
	return false;
}

//SETTING UP OUR POPUP   
//0 means disabled; 1 means enabled;   
var popupStatus = 0; 

//loading popup with jQuery magic! 
function loadPopup()
{   
	//loads popup only if it is disabled 
	if(popupStatus==0){ 
		$("#backgroundPopup").css({   "opacity": "0.8"  }); 
		$("#backgroundPopup").fadeIn("slow");
		$("#zoom").fadeIn("slow");       
		popupStatus = 1;   
	}  
}

//centering popup   
function centerPopup(){   
	//request data for centering   
	var windowWidth = document.documentElement.clientWidth;   
	//var windowHeight = document.documentElement.clientHeight;   
	var windowHeight = 416;   
	var popupHeight = $("#zoom").height();   
	var popupWidth = $("#zoom").width();  
	//centering 
	$("#zoom").css({"position": "absolute",
				 // "top": windowHeight/2-popupHeight/2, // top is calculating according to height of image
				 "left": windowWidth/2-popupWidth/2   
			});   
	//only need force for IE6  
	$("#backgroundPopup").css({   "height": windowHeight   });     }  


var current_CorbisID = '';
function view_image( imageUID, width, height, CorbisID ) {
	current_CorbisID = CorbisID;
	$('#options_1').css( 'height','0px' );
	$('#the_image').css( 'width', width + 'px' );
	$('#the_image').css( 'height', height + 'px' );
	$('#zoom').css( 'width',  width + 'px' );
	$('#zoom').css( 'height', height + 'px' );
	$('#zoom').css( 'top', (( 370 - height ) / 2 ) -20  + 'px' );
	jQuery.get( 	
	     "image.php", 
	       {imageUID: imageUID, width : width, height: height },
               function( image_link ) {
               document.getElementById('the_image').src = image_link;
		 $('#the_image').load(function(){
 		 	centerPopup(); 
			loadPopup();  
			$('.img_options').fadeIn("slow");               
		 });
 		 
		 
          } 
	);
}

function back() {
	if(popupStatus==1)
	{   
		$("#backgroundPopup").fadeOut("slow");   
		$("#zoom").fadeOut("slow");   
		popupStatus = 0; 
		$('.img_options').fadeOut("slow"); 
	}
}

function toggle_regions( region_setid ) {
	var region_set = $( '#' + region_setid );
	if(!region_set) return;

	var agent = navigator.userAgent.toLowerCase();
	var agentsCollection = {'iPhone': 'iphone',  // iPhone
				//'windowsMobile': 'windows ce',  // Windows Mobile
				'blackBerry' : 'blackberry', // BlackBerry
				'iPod' : 'ipod',         // iPod
				'symbian60' : 'series60', // Symbian S60 Smartphones
				'symbian' : 'symbian',  // Symbian S60 Smartphones
				'android' : 'android',  // Android Devices 
				'palm' : 'palm'        // PalmOS Devices 
			};

	 var isMobileDevice = false;			
	 // checking request comes from mobile device				 
        $.each(agentsCollection,function (key, value){
			if(!isMobileDevice) isMobileDevice = (agent.indexOf(value) != -1);
		});

	if (!isMobileDevice) {
	 	$('#languages').css('height','240px'); // show language option
		$('#languages ul li.Selected').mousemove(function (){$(this).attr('style','background-color:#b4b4b4;background-position:right -36px!important;');});
		$('#languages ul li.Selected').mouseleave(function (){$(this).attr('style','background-color:#CCCCCC;background-position:right -4px !important;');});
		
		$('#languages').mouseleave(function(){$('#languages').css('height','0px');});
  		
		$('#languages ul li').click(function (){
			region_switch($(this).attr('lcid'));
		});	
	} // if the reqesting device is mobile simply focus to dropdown
	else { $(region_set.get(0)).css('display', 'block');
		region_set.get(0).focus();
		}
}

function toggle_options( option_set ) {
	option_set = document.getElementById( option_set );
	if( $(option_set).css('height') != '250px' )
		$(option_set).css('height','250px');
	else $(option_set).css('height','0px');
}

function email_link() {
	link = 'http://pro.corbis.com/search/searchFrame.aspx?txt=' + current_CorbisID;
	document.location = 'mailto:?subject=Corbis%20image&body=' + link;
	$('#options_2').css('height','0px');
}

function visit_corbis() {
	link = 'http://pro.corbis.com/search/searchFrame.aspx?txt=' + current_CorbisID;
	window.open( link );
	$('#options_2').css( 'height', '40px' );
}

function add_to_home() {
	alert( home_inst );
}

function region_switch( region ) {
     document.location = '?region=' + region;
}



