var t;
var t2;
var t3;
var t4;
var noaction;


var newstickerInitialize = function ( container ) {

	var d = $(container);
	var newsticker = d.parent();
	var multiplier = 1;
	var listitems = $("ul:first li", d);
	

	if (listitems.length > 0) {
		var w = 0;
		for( i=0; i < listitems.length; i++ ) {
			w = w + $(listitems[i]).innerWidth();
		}
	}
	multiplier = Math.ceil((newsticker.width()*2) / w);
	
	if (multiplier == 1) {
		multiplier = 2;		
	}
	d.width(w*multiplier);
	$("ul:first", d).width(w);
	for ( i = 1; i < multiplier; i++ ) {
		var secondul = $("ul:first", d).clone();
		secondul.insertAfter($("ul:first", d));
	}
	d.attr("w", w);
	newstickerAnimate(d.get(0));
}

var newstickerAnimate = function ( container ) {
	d = $(container);
	d.css("left", "0px");
	scrollTicker(container, 0);
}

var scrollTicker = function ( container, position ) {
	d = $(container);
	position = position - 1;
	var leftposstring = position + "px";

	d.css("left", leftposstring);
	if (position <= (-(d.attr("w")))) {
		newstickerAnimate( container );
	} else {
		clearTimeout(t4);
		t4 = setTimeout(function() {scrollTicker(container, position)}, 40);
	}
}















var setformaction = function( item ) {
		
		if ($(item).val() == "google") {	
			$("form.headlineform").attr("action", "http://www.google.com/search");
			$("form.headlineform input.jNiceInput").attr("name", "q");
		} else {
			$("form.headlineform").attr("action", "/search");
			$("form.headlineform input.jNiceInput").attr("name", "sphinx_search300_query");

		}
	}


$(document).ready(function() {
	
	
                $("div.newsticker div.container").each( function(){
                        newstickerInitialize(this);
                });


	var photoscroller = function( anchor, order ) {
		
		var container = $(anchor).parent().parent();
		var anchors = $("div.scrolldiv a", container);		
		var newindex;	
		var newanchor;
		for(var i = 0; i < anchors.length; i++) {
			var item = $(anchors[i]);
			if ($("div", item).attr("class") == "sphotoborder_selected") {
				var index = i;	
			}
		}
		if (order) {
			if (index == 1) {
				newindex = anchors.length - 2;	
				newanchor = anchors[newindex];
				
				$("div", anchors[newindex]).attr("class", "sphotoborder_selected");
				$("div", anchors[index]).attr("class", "sphotoborder");
				selectphoto(newanchor, true, true);
				newindex = anchors.length - 3;	
			} else {
				newindex = index - 1;	
			}
		} else {
			if (index == (anchors.length-2)) {
				newindex = 1;
				newanchor = anchors[newindex];
				
				$("div", anchors[newindex]).attr("class", "sphotoborder_selected");
				$("div", anchors[index]).attr("class", "sphotoborder");
				selectphoto(newanchor, true, true);
				newindex = 2;	
			} else {
				newindex = index + 1;	
			}
		}
		
		$("div", anchors[newindex]).attr("class", "sphotoborder_selected");
		$("div", anchors[index]).attr("class", "sphotoborder");
		var newarticleid = $(anchors[newindex]).attr("name");
		newanchor = anchors[newindex];
		selectphoto(newanchor, false, true);
	}
	// NOTE: use init_select param, because we dont want to redirect on document.ready
	var selectphoto = function( anchor, fast, frombutton, init_select ) {
		// check for bad anchor
		if (!anchor || $(anchor).length == 0) {
			return false;
		}

		var articleid = $(anchor).attr("name");	
		var cucc3 = $(anchor).parent();	
		var cucc = $("div:first", $(anchor));
		var nextpos = cucc3.offset().left - cucc.offset().left;
		
		if (cucc.attr("class") == "sphotoborder_selected" && !fast && !frombutton && init_select !== true) {
			window.location = $(anchor).attr("href");
		}
		
		$("div.sphotoborder_selected", cucc3.parent()).attr("class", "sphotoborder");
		
		
		cucc.attr("class", "sphotoborder_selected");	
		nextpos = nextpos + 68;
		


		if (fast) {
			cucc3.css("left", nextpos + "px");	
		} else {
			noaction = true;	
			cucc3.animate({"left": nextpos + "px"}, 500, "swing", function() {
				noaction = false;	
				}
			);
		}
		
		var article = $("#" + articleid);
		$("div", article.parent()).hide();
		if ($("div.photoscroller_dinboxtitle", article)) {
			$("div.photoscroller_dinboxtitle h4", article.parent().parent()).text( $("span.hsubtitle", article).text());
		}
		article.show();
		//return false;	
	}
	var tabswitcher = function( tabanchor ) {
	
		var tab = $(tabanchor).attr("id");	
		var parent = $(tabanchor).parent().parent().parent();
		var container = $("div.switchertabs", parent);
		$("div[@class^='switchertab']", container).hide();
		$("div[@class*='" + tab + "']", container).show();
		$(".selected", $(tabanchor).parent()).attr("class", "");
		$(tabanchor).attr("class", "selected");
		return false;	
	}
	
	var boxswitcher = function( tabanchor ) {
	
		var tab = $(tabanchor).attr("id");	
		var parent = $(tabanchor).parent().parent().parent().parent().parent().parent().parent();
		var container = $("div.switcherboxes", parent);
		
		$("div[@class^='switcherbox']", container).hide();
		$("div[@class*='" + tab + "']", container).show();
		
		$(".boxoption_selected", $(tabanchor).parent()).attr("class", "boxoption");
		$(tabanchor).attr("class", "boxoption_selected");
		return false;	
	}
	
	var searchclick = function( button ) {
		return false;
	}
	
	var articleswitcher = function( anchor, order ) {
		


		clearTimeout(t);
		//var container = $(anchor).parent().parent().parent().parent();
		//var items = $("div.photopager_images img", container);		
		
		var items = $("div[name='photopager']");
		
		
		
		var newindex;	
		for(var i = 0; i < items.length; i++) {
			var item = $(items[i]);
			if ($(item).attr("class") == "visible") {
				var index = i;	
			}
		}
		if (order) {
			if (index == 0) {
				newindex = items.length - 1;	
			} else {
				newindex = index - 1;	
			}
		} else {
			if (index == (items.length-1)) {
				newindex = 0;	
			} else {
				newindex = index + 1;	
			}
		}
			
		$(items).attr("class", "hidden");
		
		$(items).hide();
		
		//container.parent().parent().parent().fadeIn("slow");
		
		$(items[newindex]).fadeIn("slow");
		$(items[newindex]).attr("class", "visible");
			
		/*$(items[newindex]).fadeIn(300, function () {
			$(items[newindex]).attr("class", "visible");
		});
	*/

		/*container.parent().parent().parent().hide();
		

		$(items[newindex]).attr("class", "visible");
				
		var textitems = $("div.text_container div[@name^='lead']", container.parent().parent());		
		
			
		$(textitems).attr("class", "hidden");
		
		$(textitems[newindex]).attr("class", "visible");
		*/
		
		/*
		$(textitems[newindex]).fadeIn(300, function () {
			$(textitems[newindex]).attr("class", "visible");
		});
		*/

		//container.parent().parent().parent().fadeIn("slow");
			

		//$(textitems[newindex]).attr("class", "visible");

		/*$("div", anchors[newindex]).attr("class", "sphotoborder_selected");
		$("div", anchors[index]).attr("class", "sphotoborder");
		var newarticleid = $(anchors[newindex]).attr("name");
		var newanchor = anchors[newindex];
		*/



		/*	


		b = $(anchor).attr("class");	
		a = $(anchor).parent().parent().parent().parent().attr("id");
			

		$("#"+a).hide();
		$("#"+b).fadeIn("slow");
		*/
		return false;
	}

	
	$(".scrolldiv").each(function() {
		var d = $(this);
		var anchors = $("a", d);		
		
		
		
		if (anchors.length == 1) {	
			var first1 = anchors[0];
			$(first1).clone(true).appendTo(d);	
			$(first1).clone(true).appendTo(d);	
			$(first1).clone(true).appendTo(d);	
			$(first1).clone(true).appendTo(d);	
			$(first1).clone(true).appendTo(d);	
			var anchors = $("a", d);		
			selectphoto(anchors[1], null, null, true);
		} else if (anchors.length == 2) {	
			var first1 = anchors[0];
			var first2 = anchors[1];
			$(first1).clone(true).appendTo(d);	
			$(first2).clone(true).appendTo(d);	
			$(first1).clone(true).appendTo(d);	
			$(first2).clone(true).appendTo(d);	
			$(first1).clone(true).appendTo(d);	
			selectphoto(first2, null, null, true);
				
		} else if (anchors.length > 2) {	
			
			var first1 = anchors[0];
			var first2 = anchors[1];
			var first3 = anchors[2];
			
			$(first1).clone(true).appendTo(d);	
			$(first2).clone(true).appendTo(d);	
			$(first3).clone(true).appendTo(d);	
		
			selectphoto(first2, null, null, true);
		}





	});
		
	
	
	
	
	$(".photoscroller_buttons, .photoscroller_buttons").each(function() {
		var d = $(this);

		$("a.prev", d).bind("click", function() {
			if (!noaction)	photoscroller(this, true);
			return false;
		});

		$("a.next", d).bind("click", function() {
			//if (!noaction) 
			photoscroller(this, false);
			return false;
		});
	});

	$(".scrolldiv a.photo").each(function() {
		
		var d = $(this);
		$(d).bind("click", function() {
			
			if (!noaction) {	
			var target = d; 
			var container = d.parent();
			var anchors = $("a.photo", container);	
			
			if (anchors[0] == d.get(0)) {
			
				newindex = anchors.length - 2;
				newanchor = anchors[newindex];
				
				$("div", anchors[newindex]).attr("class", "sphotoborder_selected");
				$("div", d).attr("class", "sphotoborder");
				selectphoto($(newanchor), true);
				target = $(anchors[newindex-1]);
			} else if (anchors[anchors.length-1] == d.get(0)) {
				newindex = 1;	
				newanchor = anchors[newindex];
				
				$("div", anchors[newindex]).attr("class", "sphotoborder_selected");
				$("div", d).attr("class", "sphotoborder");
				selectphoto($(newanchor), true);
				target = $(anchors[newindex+1]);
			} else {
				//window.location = "#";
			}
			selectphoto(target);
			}
			return false;
		});
	});


	
	$("#listquests").each(function() {
		var d = $(this);	
		$(d).bind("click", function() {
			var link;	
			link = $("#pageurl").val() + "/M" + $("#monthsel").val() + "Y" + $("#yearsel").val();
			window.location = link;
			return false;
		});
	});
	
	$(".tabswitchers a").each(function() {
		var d = $(this);	
		$(d).bind("click", function() {
			return tabswitcher(this);
		});
	});
	
	$(".boxswitchers a").each(function() {
		var d = $(this);	
		$(d).bind("click", function() {
			return boxswitcher(this);
		});
	});
	
	$("div.photopager_bg").each(function() {
		var d = $(this);	
		
		$("div[name='photopager']").attr("class", "hidden");
		$("div[name='photopager']:first").attr("class", "visible");
		
		
		$("a.prev", d).bind("click", function() {
			return articleswitcher(this, true);
		});

		$("a.next", d).bind("click", function() {
			return articleswitcher(this, false);
		});
	});
		
	$("div.articlebox").each(function() {
		var d = $(this);	
		var title = $("h4", d);	

		//var contenttext = $(".content", d).text();	
		
		if ($(".content", d).length) {
			var content = $(".content", d).attr("class", "innerbox");
		} else {
			var content = $(document.createElement("div")).attr("class", "innerbox").text(d.text()); 
		}
		
			
		var content1 = document.createElement("div");
		var content2 = document.createElement("div");
		var content3 = document.createElement("div");
		var content4 = document.createElement("div");
		
		
		$(content1).attr("class", "content");
		$(content4).attr("class", "content4").append(content);
		$(content2).attr("class", "content2");
		$(content3).attr("class", "content3").append(title).append(content4);
		
		$(d).empty();		
		$(d).append(content1);
		$(d).append($(content1));
		$(content1).append($(content2));	
		$(content2).append($(content3));	
	});

	var timerpaging = function() {
		var items = $("div.photopager_bg");
		var container = $("div[name='photopager'].visible");
		var nextbutton = $("a.next", container);
		articleswitcher(nextbutton, true);
		clearTimeout(t);
		t = setTimeout( timerpaging, 20*1000 );	
	}
	
	var timerscrolling = function() {
		var nextbuttons = $("a.autonext");
		$("a.autonext").trigger('click');
		clearTimeout(t2);
		t2 = setTimeout( timerscrolling, 10*1000 );	
	}
	
	var timerswitcher = function() {
		
		var containers = $("div.autoswitcher"); 
		if (containers.length > 0) {
			for(var i = 0; i < containers.length; i++) {
				var anchors = $("a", containers[i]);

				if (anchors.length > 0) {
					var selected = 0;	
					for(var z = 0; z < anchors.length; z++) {
						if ( $(anchors[z]).hasClass("boxoption_selected") ) {
							selected = z;	
						}
								
					}
					if (selected == (anchors.length -1)) {
						var nextselected = 0;			
					} else {
						var nextselected = selected + 1;
					}
					$(anchors[nextselected]).trigger('click');
				}
			}
		}
		
		clearTimeout(t3);
		t3 = setTimeout( timerswitcher,  5*1000 );	
	}
	
	timerpaging();
	timerscrolling();
	timerswitcher();

 	jscroller2.refreshtime = 70;






});






