$(function() {
	var img_cnt_pg = 1;
	var img_cnt_es = 1;
	
	while( $('a[rel*="lightbox-pg-' + img_cnt_pg + '"]').size() != 0 ){
		$('a[rel*="lightbox-pg-' + img_cnt_pg + '"]').lightBox(); // photo gallery
		img_cnt_pg++;
	}

	while( $('a[rel*="lightbox-es-' + img_cnt_es + '"]').size() != 0 ){
		$('a[rel*="lightbox-es-' + img_cnt_es + '"]').lightBox(); // e-store
		img_cnt_es++;
	}

	$('a[rel*=facebox]').facebox();
	
	// ultra-mega dirty hack bc we didn't used html as we should, and we pay the price of doing things wrong
	$('#cal_date_selector').after('<div id="cal_div_hidden" style="display: none;"><h3>&nbsp;</h3></div>');
	$('.cal_evt_popup').css('font-size', $('#cal_div_hidden h3').css('font-size'));
	$('.cal_evt_popup').css('font-weight', $('#cal_div_hidden h3').css('font-weight'));
	$('.cal_evt_popup').css('color', $('#cal_div_hidden h3').css('color'));
	$('.cal_evt_popup').css('text-decoration', 'none');
	$('#cal_div_hidden').remove();

	if ($.browser.msie && $.browser.version == '6.0') {
		$('a[id*="cal_event_gmap_view_"]').html('Upgrade to Internet Explorer 7 or 8 to see the map.').attr( { href: 'http://www.microsoft.com/windows/internet-explorer/', target: '_blank' } );
	}

	$('a[id*="cal_event_gmap_view_"]').click( function(){
		var link_number;
		link_number = $(this).attr('id').replace('cal_event_gmap_view_', '');
		
		if ( $('#cal_event_map_status_' + link_number).val() == 0 ) { // map is hidden
			$('#cal_event_map_status_' + link_number).val('1');
			$('#cal_event_gmap_view_' + link_number).html('Hide Map');
			$('#cal_event_gmap_container_' + link_number).html($('#cal_event_gmap_code_' + link_number).val()).show();
			
		} else { // map is currently shown
			$('#cal_event_map_status_' + link_number).val('0');
			$('#cal_event_gmap_view_' + link_number).html('View Map');
						
			$('#cal_event_gmap_container_' + link_number).html('').hide();
		}
	});

	try {
		// Safari allows interdomain access, so we need to catch it
		if(top.window.usersite) return false;
	} catch (err) {
		return false;
	}
	
	if(player_active != true) return false;

	try {
		if(top.window.mainFrame != null){
			top.document.title = top.window.frames['mainFrame'].document.title; // update frame title
			return false;
		}
	} catch (err) {
		return false;
	}

	if(!$.browser.opera){
		$('body').remove();

	    $(window).resize(function(){
	        $("#mainFrame").css('height', $(window).height() - 31);
	    });
	
		$('html').append( 
			$('<frameset>').attr({
				'frameborder': '0',
				'framespacing': '0',
				'id': 'fset',
				'rows': '*,31',
				'cols': '*'
		}) );
	
		$('#fset').append($('<frame />').attr({	'src': mainframe_url, 'scrolling': (($.browser.msie) ? 'yes' : 'auto'), 'name': 'mainFrame', 'id': 'mainFrame' }).css('height', $(window).height() - 31));
		$('#fset').append($('<frame />').attr({	'src': player_url, 'scrolling': 'no', 'name': 'playerFrame', 'id': 'playerFrame' }));
		
		top.window.frames['mainFrame'].document.location = mainframe_url;
	}else{
		var data = 	'<frameset frameborder="0" framespacing="0" id="fset" rows="*,31" cols="*">'
				+ 	'<frame src="' + mainframe_url + '" name="mainFrame" id="mainFrame" />'
				+	'<frame src="' + player_url + '" name="playerFrame" id="playerFrame" />'
				+	'</frameset>';
		
		document.write(data);
	}
});
