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

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


	$("#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://www.iiinf.com", "بانک اطلاعات صنايع ايران");
	});

	$("#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: 300
	});

	// 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 changeTheme(obj)
{
	$.cookie("iiinf_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.htm";
	var _searchBox = $("#searchInput");
	window.location.href = searchLocation + "?psearch=" + _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 replaceAll(_str, _oldChar, _newChar)// :String;
{
	return _str.split(_oldChar).join(_newChar);
};


