/**
 * Javascript functions
 * @author Adrian7 (http://adrian.silimon.eu)
 * @uses jQuery 1.2.4
 * @version 1.0
 */


var player_state 	  = "auto";
var frame_fix_timeout = null;
var _doc_height 	  = null;

function set_cookie( name, value, expires, path, domain, secure ){
	
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime(today.getTime());
	
	/*
	if the expires variable is set, make the correct
	expires time, the current script below will set
	it for x number of days, to make it for hours,
	delete * 24, for minutes, delete * 60 * 24
	*/
	if ( expires ){
		expires = expires * 1000 * 60;
	}
	
	var expires_date = new Date( today.getTime() + (expires) );
	
	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
	( ( path ) ? ";path=" + path : "" ) +
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}

function get_cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 ){
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}

function set_player_state(state, cookie_set){
	player_state = state;
	
	if(cookie_set){
		set_cookie('player_state', state, 25, '/', '.weewerethere.com', false);
		set_cookie('player_state', state, 25, '/galleries', '.weewerethere.com', false);
	}
}

function get_player_state(){
	player_state = get_cookie('player_state');
	
	return player_state;
}

function fix_frame_height(){
	
	var ParentWin = window.parent;
	var ParentTop = window.top;
	
	try{
		_doc_height = $('body table:first').get(0).height();
	}
	catch(e){
		try{
			_doc_height = $('body table:first').height();
		}
		catch(e){
			_doc_height = $('body table').get(0).height();
		}
	}
	
	try{
		ParentWin.set_frame_height(_doc_height);
	}
	catch(e2){ 
		try{
			ParentTop.set_frame_height(_doc_height);
		}
		catch(e5){
			//alert("Exception: (E2) " + e5.message);
		}
	}
	
	clearTimeout(frame_fix_timeout);
	
	frame_fix_timeout = setTimeout('fix_frame_height()', 1300);
}

function load_images(){
	var scroll_height= $(window).scrollTop();
	var win_height   = $(window).height();
	
	//all images having longdesc attribute, should be loaded 
	$('body img.wait').each(function(){
		var image_url = $(this).attr('longdesc');
			
		if((typeof image_url == 'string') && (image_url.length > 0)){
			//check the image position and scroll height
			var img_position = $(this).offset();
			
			if(img_position.top <= (scroll_height + win_height)){
				$(this).attr("src", image_url);
				$(this).removeClass('wait');
			}
		}
	});
}

function open_music_player(){
	window.open("flashmp3/player.php", "Player", "width=200,height=100");
	set_player_state('open', true);
}

function auto_open_music_player(){
	if(player_state != 'open'){		
		$('a.player_open:first').click();
	}
}

$(document).ready(function (){
	load_images();
	
	get_player_state();
	
	$('a.player_open').click(function(){
		open_music_player();
	});
	
	//also bind event to window scroll
	$(window).scroll(function(){
		load_images();
	});
	
	setTimeout('auto_open_music_player()', 3000);
});

