﻿// Rasekhoon page scripts

$(function () {
	$("#loginpanel").hide();
	$("#themepanel").hide();

	CurrentPanels_array = new Array();
	AvailablePanles_array = new Array();

	try {
		if (DefaultPage) {
			$("#patch").click(function () {
				if ($('#setting').css('top') == '0px') {
					$('#setting').animate({ top: '-85px' }, 'fast', 'swing');
				}
				else {
					$('#setting').animate({ top: '0px' }, 'slow', 'swing');
					$("#loginpanel").animate({ bottom: '-70px' }, 'fast');
				}
			});
			$("#patch").css('background-image', 'url(/_img/setting_patch.png)');
			$("#patch").attr("title", "تنظیمات صفحه");
			setTimeout(function () {
				$("#suggestion").animate({ top: '-10px' }, 'fast');
			}, 3000);
		}
	}
	catch (e) {
		$("#patch").click(function () {
			$.cookie('ew-position', null);
			location.reload();
		});
		var CookieValue = $.cookie('ew-position');
		if (CookieValue != "" && CookieValue != null) {
			CookieValue = replaceAll(CookieValue, "p91", "");
			CookieValue = replaceAll(CookieValue, ",,", ",");
			CookieValue = replaceAll(CookieValue, "palceleft=", "palceleft=p91,");
			$.cookie('ew-position', CookieValue);
		}
		$("#patch").css('background-image', 'url(/_img/setting_reset.png)');
		$("#patch").attr("title", "تنظیمات پیش فرض صفحه");
	}


	$("#patch").animate({ "opacity": "show" }, "slow");


	$("#setting-reset").click(function () {
		$("#patch").click();
		$.cookie("ew-position", null);
		$.cookie("Rasekhoon_Theme", null);
		location.reload();
	});


	$("#setting-save").click(function () {

		var panels = $('#sections input');
		var placeright = "placeright=p20,p27,p30,";
		var placeleft = "placeleft=p91,";
		var places = null;
		if (CurrentPanels_array.length > 0) {
			for (var i = 0; i < panels.length; i++) {
				var myPanle = $(panels[i]).attr('name');
				if (myPanle == "p91" || myPanle == "p20" || myPanle == "p27" || myPanle == "p30")
					continue;

				if ($(panels[i]).attr('checked')) {
					if ($(panels[i]).val() == "0") {
						placeleft += $(panels[i]).attr('name') + ",";
					}
					else {
						placeright += $(panels[i]).attr('name') + ",";
					}
				}
			}

			placeleft = placeleft.substring(0, placeleft.length - 1);
			placeright = placeright.substring(0, placeright.length - 1);

			places = placeright + "|" + placeleft;
		}

		$("#patch").click();
		$.cookie('ew-position', places);
		window.location = "/?.rand=" + Math.random();
	});

	$("#searchBtn").click(function () {
		doSearch();
	});


	$("#login").click(function () {
		$("#loginpanel").show();
		if ($('#loginpanel').css('bottom') == '-70px') {
			$("#loginpanel").animate({ bottom: '26px' }, 'slow', 'swing', function () { $("#login-username").focus(); });
			$("#themepanel").animate({ bottom: '-35px' }, 'fast', function () { $("#themepanel").hide() });
			$('#setting').animate({ top: '-85px' }, 'fast');
		}
		else {
			$("#loginpanel").animate({ bottom: '-70px' }, 'slow', function () { $("#loginpanel").hide() });
		}
	});

	$("#theme").click(function () {
		$("#themepanel").show();
		if ($('#themepanel').css('bottom') == '-35px') {
			$("#themepanel").animate({ bottom: '10px' }, 'slow', 'swing');
			$("#loginpanel").animate({ bottom: '-70px' }, 'fast', function () { $("#loginpanel").hide() });
		}
		else {
			$("#themepanel").animate({ bottom: '-35px' }, 'slow', function () { $("#themepanel").hide() });
		}
	});

	$("#theme-green").click(function () {
		var obj = new Object();
		obj.name = "green";
		changeTheme(obj);
	});

	$("#theme-blue").click(function () {
		var obj = new Object();
		obj.name = "blue";
		changeTheme(obj);
	});

	$("#theme-gray").click(function () {
		var obj = new Object();
		obj.name = "gray";
		changeTheme(obj);
	});

	$("#theme-violet").click(function () {
		var obj = new Object();
		obj.name = "violet";
		changeTheme(obj);
	});


	$("#bookmark").click(function () {
		bookmark("http://rasekhoon.net", "راسخون - یار همیشه همراه");
	});

	$("#sug").click(function () {
		if ($("#suggestion").css('top') == '-110px') {
			$("#suggestion").animate({ top: '-10px' }, 'fast');
		}
		else {
			$("#suggestion").animate({ top: '-110px' }, 'fast');
		}

	});

	// jCarouselLite script
	$("#news").jCarouselLite({
		vertical: true,
		hoverPause: true,
		visible: 1,
		auto: 5000,
		speed: 500
	});

	// EasyWidgets Scripts
	$.fn.EasyWidgets({
		effects: {
			effectDuration: 300,
			widgetShow: 'slide',
			widgetHide: 'slide',
			widgetClose: 'slide',
			widgetExtend: 'slide',
			widgetCollapse: 'slide',
			widgetOpenEdit: 'slide',
			widgetCloseEdit: 'slide',
			widgetCancelEdit: 'slide'
		}
		,
		callbacks: {
			onClose: function (link, widget) {
				alert('Close the panel: ' + widget.attr('id'))
			}
		}

	});

	$(".more").click(function (e) {
		setCommentRate(this.id, 1);
	});

	$(".less").click(function (e) {
		setCommentRate(this.id, -1);
	});

	setting_pre();
});


function setCommentRate(cid, rate)
{
	var cid = cid.substr(2, cid.length);

	type = $("#cr" + cid + " span").attr('xid');

	if ($.cookie(type + cid) == null)
	{
		$.cookie(type + cid, "1");
		$.ajax({ url: "/Members/comment.aspx?type=" + type + "&cid=" + cid + "&rate=" + rate, success: function (data) {
				/*
				if (parseInt(data) > 0)
					data = "<font color=#33cc33>" + data + "</font>";
				else if (parseInt(data) < 0)
					data = "<font color=#ff6666>" + data + "</font>";
				*/
				$("#cr" + cid + " span").html(data);
			}
		});
	}
	else
	{
		alert("کاربر گرامی رای شما برای این نظر، قبلا ثبت شده است");
	}
	
}


function changeTheme(obj)
{
	$.cookie("Rasekhoon_Theme", obj.name, {path:"/", expires:365});
	$(".themeStyle").attr("href", "/_doc/"+obj.name+".css");
	//ribbonBack
	$("#logo img").attr("src", "/_img/header_logo_"+obj.name+".png");
	$("#themepanel").animate({bottom:'-35px'},'slow',function(){$("#themepanel").hide()});
}

function setting_pre()
{
	var positions = $.cookie('ew-position');
	if( (positions != null) )
	{
	  
      // Get the widgets places IDs and widgets IDs
      var places = positions.split("|");
      var totalPlaces = places.length;
      for(var i = 0; i < totalPlaces; i++){
        // Every part contain a place ID and possible widgets IDs
        var place = places[i].split('=');
        // Validate (more or less) the format of the part that must
        // contain two element: A place ID and one or more widgets IDs
        if(place.length == 2){
          // Subpart one: the place ID
          var placeSel = '#'+place[0];
          // Subpart two: one or more widgets IDs
          var widgets = place[1].split(',');
          var totalWidgets = widgets.length;
          // Here we have a place and one or more widgets IDs
          for(var j = 0; j < totalWidgets; j++){
            if($.trim(widgets[j]) != ''){
              // So, append every widget in the appropiate place
              var widgetSel = ''+widgets[j];
			  //alert(widgetSel);
			  CurrentPanels_array.push(widgetSel);
              //$(widgetSel).appendTo(placeSel);
            }
          }
        }
      }
    }
	

	
	var panels = $('#sections input');
	
	if(CurrentPanels_array.length > 0)
	{
		for(var i=0; i<panels.length; i++)
		{
			AvailablePanles_array.push("#"+panels[i].name);
			if($.inArray(panels[i].name, CurrentPanels_array) != -1)
				$(panels[i]).attr('checked', true);
		}
	}
};

function suggestionAlerter(action)
{
	if(action == 'start')
	{
		SuggestionInterval = setInterval(function(){
			$("#sug").css('background-position', '(-182px 26px)');
			$("#sug").css('background-position', '(-182px 0px)');
			$("#sug").css('background-position', '(-182px 26px)');
			$("#sug").css('background-position', '(-182px 0px)');
		},2000);
	}
	else
	{
		$("#sug").css('background-position', '(-182px 0px)');
		SuggestionInterval = null;
	}
}


function bookmark(url,title)
{
	if (window.sidebar) {return window.sidebar.addPanel(title,url,"")}
	else if (window.external) {return window.external.AddFavorite(url,title)}
	else if (window.opera && window.print) {return true};
	return false;
};

function doSearch()
{
	var selectedSubject = "Article";
	var searchLocation = "/search/";
	var _searchBox = $("#searchInput");
	window.location.href = searchLocation + "?Page=1&Words=" + escape(_searchBox.val()) + "&SWhere=" + selectedSubject + "&SData=subjects&SType=ExactSentence";
};

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 replaceAll(_str, _oldChar, _newChar)// :String;
{
	return _str.split(_oldChar).join(_newChar);
};

function OstanChange(OstanID) {

	var DL = document.getElementById("ShahrDL");
	clearOptions(DL);

	var optn = document.createElement("OPTION");
	optn.text = "لطفا منتظر بمانيد";
	optn.value = "-1";
	DL.options.add(optn);
	AjaxGetVal(OstanID, '', 1);
}
function OstanFill(CityStr) {
	if (CityStr == "")
		return;
	var DL = document.getElementById("ShahrDL");
	var ostanTXT = CityStr;
	clearOptions(DL);
	while (ostanTXT.length != 0) {
		var endL = ostanTXT.indexOf("&");
		var tmpit = ostanTXT.substr(0, endL);
		ostanTXT = ostanTXT.substr(endL + 1);

		var optn = document.createElement("OPTION");
		optn.text = tmpit.substr(tmpit.indexOf('#') + 1);
		optn.value = tmpit.substr(0, tmpit.indexOf('#'));
		DL.options.add(optn);
	}
	CityChange(document.getElementById("ShahrDL").value)
}
function CityChange(shahrid) {
	document.getElementById("CityTB").value = document.getElementById("ShahrDL").value;
}

var objXMLHTTP = null;
var objXMLHTTPpop = null;
function AjaxGetVal(Myval, stat, MyType) {

	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		objXMLHTTP = new XMLHttpRequest();
		objXMLHTTPpop = new XMLHttpRequest();
	}
	if (window.ActiveXObject) { // IE
		try {
			objXMLHTTP = new ActiveXObject("Msxml2.XMLHTTP");
			objXMLHTTPpop = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				objXMLHTTP = new ActiveXObject("Microsoft.XMLHTTP");
				objXMLHTTPpop = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e1) {
				objXMLHTTP = null;
				objXMLHTTPpop = null;
			}
		}
	}
	var strOutput;

	strOutput = 'Error';
	objXMLHTTP.open("GET", 'ajax.aspx?MyVal=' + Myval + '&MyDiv=' + stat + '&MyType=' + MyType);
	objXMLHTTP.onreadystatechange = AjacBack;
	objXMLHTTP.send(null);
}
function AjacBack() {
	var strout = "";
	if (objXMLHTTP.readyState == 4 && objXMLHTTP.status == 200) {
		strOutput = objXMLHTTP.responseText;
		OstanFill(strOutput);
	}
}
function clearOptions(list) {
	var i;
	for (i = list.options.length - 1; i >= 0; i--) {
		list.remove(i);
	}
}
