﻿var _global = new Object();

$(function () {
	// jCarouselLite script
	$("#headlines").jCarouselLite({
		vertical: true,
		hoverPause: true,
		visible: 1,
		auto: 3500,
		speed: 500
	});

	$("#ribbon-footer").jCarouselLite({
		vertical: true,
		hoverPause: true,
		visible: 1,
		auto: 4000,
		speed: 500
	});


	$("#search_btn").click(function () {
		doSearch();
	});

	panelStyle();

	_global.intervals = new Array();
	tablinks();

	$("#navmenu-h li,#navmenu-v li").hover(
		function () { $(this).addClass("iehover").show(1000); },
		function () { $(this).removeClass("iehover"); }
	);
	/*
	$(".body img").error(function () {
		$(this).attr("src", "/_img/noimage.gif");
	})
	*/
	$(".comment .response").each(function () {
		var response = $(this);
		if (jQuery.trim(response.html()) != "") {
			response.html("<b style='color:red'>پاسخ مدیر :</b><br />" + response.html());
		}
		else {
			response.hide();
		}
	})

});

function tablinksTimer(panel) {

	var circles = panel.find(".tablinks");
	var circle_active = circles.find(".circle-active");
	var circle_index = circles.find(".circle-active").index();
	var circle_size = circles.find(".circle").size();

	if (circle_index == circle_size - 1) {
		var circle_active = circles.find(".circle").first();
		circle_active.click();
	}
	else {
		circle_active.next().click();
	}
}

function tablinks() {

	$(".tabular").each(function (index, element) {

		var panel = $(this);
		var bodies = $(this).find(".body");

		$(bodies).hide();
		$(bodies).first().show();

		var tablinks_div = document.createElement('div');
		$(tablinks_div).addClass("tablinks");

		var bodyCount = $(bodies).size();

		//if (bodyCount <= 1) return false;

		for (var i = 0; i < bodyCount; i++) {
			$(tablinks_div).append($("<div class='circle' />"))
		}

		$(tablinks_div).appendTo(panel);

		var circles = $(this).find(".tablinks div");

		$(circles).first().addClass("circle-active");

		if (panel.attr("timer")) {
			var timer = parseInt(panel.attr("timer")) * 1000;

			_global.intervals[index] = setInterval(function () { tablinksTimer(panel) }, timer);

			$(bodies).mouseenter(function () {
				var panel = $(this).parent()
				//clearInterval(_global.intervals[index]);
				//_global.intervals[index] = null;
			});

			$(bodies).mouseleave(function () {
				var panel = $(this).parent()
				var timer = parseInt(panel.attr("timer")) * 1000;

				//clearInterval(_global.intervals[index]);
				//_global.intervals[index] = null;
				//_global.intervals[index] = setInterval(function () { tablinksTimer(panel) }, timer);
			});
		}

		circles.click(function () {

			if ($(this).hasClass("circle-active"))
				return false;

			$(circles).removeClass("circle-active");
			$(bodies).slideUp("fast");

			var currentBody = $(bodies).get($(this).index());
			$(currentBody).slideDown("fast");

			$(this).addClass("circle-active");

			var panel = $(this).parent()
			if (panel.attr("timer")) {
				var timer = parseInt(panel.attr("timer")) * 1000;
				clearInterval(_global.intervals[index]);
				_global.intervals[index] = null;
				_global.intervals[index] = setInterval(function () { tablinksTimer(panel) }, timer);
			}
		});
	});
}


function panelStyle() {
	var panelHeaders = $('#main .panel .header');
	panelHeaders.each(function (e) {
		$(this).html("<div class=ptledge></div><div class=ptredge></div>" + $(this).html());
	});

	var panel = $('#main .panel');
	panel.each(function (e) {
		$(this).html("<div class=pbledge></div><div class=pbredge></div>" + $(this).html());
	});
}



function doSearch() {
	var searchLocation = "/search/doit/";
	var _searchBox = $("#search_box");
	window.location.href = searchLocation + unescape(_searchBox.val());
}

function doitSearch() {
	var searchLocation = "/search/doit/";
	var _searchBox = $("#search_box_l");
	window.location.href = searchLocation + unescape(_searchBox.val());
}


function isEnterPressed(e) {
	var _char
	if (e && e.which) {
		e = e;
		_char = e.which; // NN4 specific code
	}
	else {
		e = event;
		_char = e.keyCode; // IE specific code
	}
	if (_char == 13) return true;   // Enter key is 13
	else return false;
}

function checkCookie() {
	$.cookie('test_cookie', 'cookie_value', { path: '/' });
	if ($.cookie('test_cookie') == 'cookie_value') {
		return true
	} else {
		return false;
	}
}

