/* GENERIC MODAL WINDOW FUNCTION */
function modal_window(url)
{
	send_request(true, url, 'modalwindow');
	$("#modalwindow").show();
	$('#fade').slideDown(350);
}

function hide_modal()
{
	$("#modalwindow").hide();
	$('#fade').slideUp(350);
}

/* PREVIOUS MAGAZINE ISSUES DROP DOWN */
function the_magazine()
{
	var display = $('#the_magazine').css("display");
	
	if(display == 'none')
	{
		$('#the_magazine').slideDown(700);
		$('#the_magazine_image').attr({ 
          src: "/assets/images/prev-issues.gif"
        });
	}
	else
	{
		$('#the_magazine').slideUp(700);
		$('#the_magazine_image').attr({ 
          src: "/assets/images/prev-issue-show.gif"
        });
	}
}



/* TOP STORIES TABS AND ROTATION */
function top_stories(show, imagefile)
{
	$('.top-stories-txt').hide();
	$('.top-stories-txt2').hide();
	$('.top-stories-txt3').hide();
	$('.top-stories-txt4').hide();
	$('#topstory'+show).show();
	$('#tscontent a').removeClass("on"); /* remove selected class */
	change_top_story_image(imagefile);
	$('#link'+show).addClass("on"); /* add selected class */

}

function change_top_story_image(imagefile)
{
	//$('#top_story_container').fadeOut(600); /* nice transition out */
	$('#tsimg').css({'background' : '#959595 url(\''+imagefile+'\') top left'});
	//$('#top_story_container').fadeIn(600); /* nice transition in */
}
	
function enable_top_stories_rotation()
{
	if(rotation_enabled)
	{
		setTimeout("rotate_top_stories("+current_pos+")", 7500);
	}
}

function rotate_top_stories(current_pos)
{
	if(rotation_enabled)
	{
		if(current_pos < max_pos-1)
		{
			current_pos += 1;
		}
		else
		{
			current_pos = 0;
		}
		top_stories(current_pos, story_images[current_pos], story_headings[current_pos]);
		setTimeout("rotate_top_stories("+current_pos+")", 7500);
	}
}


/* BANNER ROTATION */
function enable_rotate_banner(hcurrent_banner_pos, package, id)
{
	setTimeout("rotate_banner("+hcurrent_banner_pos+", "+package+", '"+id+"')", 22000);
}

function rotate_banner(hcurrent_banner_pos, package, id)
{
	if(hcurrent_banner_pos < hmax_banner_pos-1)
	{
		hcurrent_banner_pos += 1;
	}
	else
	{
		hcurrent_banner_pos = 0;
	}
	
	//$('#'+id).hide();
	send_request(true, '/xmlhttp/get_banner.php?package='+package+'&pos='+hcurrent_banner_pos, id);
	$('#'+id).fadeIn(300);
	setTimeout("rotate_banner("+hcurrent_banner_pos+", "+package+", '"+id+"')", 22000);
}


function fenable_rotate_banner(fcurrent_banner_pos, package, id)
{
	setTimeout("frotate_banner("+fcurrent_banner_pos+", "+package+", '"+id+"')", 22200);
}

function frotate_banner(fcurrent_banner_pos, package, id)
{
	if(fcurrent_banner_pos < fmax_banner_pos-1)
	{
		fcurrent_banner_pos += 1;
	}
	else
	{
		fcurrent_banner_pos = 0;
	}
	
	//$('#'+id).hide();
	send_request(true, '/xmlhttp/get_banner.php?package='+package+'&pos='+fcurrent_banner_pos, id);
	$('#'+id).fadeIn(300);
	setTimeout("frotate_banner("+fcurrent_banner_pos+", "+package+", '"+id+"')", 22000);
}


/* SITE WIDE FUNCTIONS BELOW */
function reset_value(object, id)
{
	return object.value = '';
}

function set_value(id, value)
{
	return document.getElementById(id).value = value;
}

function hide_element(id)
{
	$('#'+id).hide();
}

function show_element(id)
{
	$('#'+id).show();
}

function get_value(id)
{
	return document.getElementById(id).value;
}

function get_selected_value(objid)
{
	return objid.options[objid.selectedIndex].value;
}

function other_element(theobject, theid, othervalue)
{
	if(get_selected_value(theobject) == othervalue)
	{
		show_element(theid);
	}
	else
	{
		hide_element(theid);
	}
}

function send_to_friend(the_title, the_link)
{
	var name = get_value('name');
	var email = get_value('email');
	var note = get_value('unote');
	modal_window('/xmlhttp/send_to_friend.php?title='+the_title+'&link='+the_link+'&name='+name+'&email='+email+'&note='+note);
}

function submit_comment_report(id, mag, article)
{
	reason = get_selected_value(document.getElementById('rreason'));
	comment = get_value('rcomment');
	modal_window('/xmlhttp/report_comment.php?id='+id+'&mag='+mag+'&article='+article+'&reason='+reason+'&comment='+comment);
}

function submit_blog_comment_report(id, blog)
{
	reason = get_selected_value(document.getElementById('rreason'));
	comment = get_value('rcomment');
	modal_window('/xmlhttp/report_blog_comment.php?id='+id+'&blog='+blog+'&reason='+reason+'&comment='+comment);
}


function submit_form ( id, url )
{
	var theForm = document.forms[id];
	var query_string = "";
	for(i=0; i<theForm.elements.length; i++)
	{
		if(theForm.elements[i].type == "text" || theForm.elements[i].type == "password" || theForm.elements[i].type == "textarea" || theForm.elements[i].type == "hidden"){
			query_string += theForm.elements[i].name + "=" + theForm.elements[i].value + '&';
		}
		else if(theForm.elements[i].type == "radio" && theForm.elements[i].checked){
			query_string += theForm.elements[i].name + "=" + theForm.elements[i].value + '&';
		}
		else if(theForm.elements[i].type == "checkbox" && theForm.elements[i].checked){
			query_string += theForm.elements[i].name + "=" + theForm.elements[i].value + '&';
		}
		else if(theForm.elements[i].type == "select-one"){
			query_string += theForm.elements[i].name + "=" + theForm.elements[i].options[theForm.elements[i].selectedIndex].value + '&';
		}
	}
	
	modal_window(url+'?'+query_string);
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

<!--
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function moveFlashWindowUp() {
	setTimeout("$('#collapsible-player').animate({'height':'28px'}, 500)", 10);
}
	
function moveFlashWindowDown() {
	setTimeout("$('#collapsible-player').animate({'height':'310px'}, 500)", 10);
}
$(function(){
		$('#tabs').tabs();		
});