/*
 * $Id$
 * The Following license and copyright information must not be removed
 *
 ******************************************************************************
 * Software Name: The Pacer Edition CMS                                       *
 *    Build Date: December 29,2009                                            *
 *    Build Time: 17:00:00                                                    *
 *     File Name: frontend.js                                                 *
 *           URL: http://www.jcwebden.com                                     *
 *                                                                            *
 ******************************************************************************
 *    The Pacer Edition CMS Copyright License:                                *
 *    Copyright 2009 The Pacer Edition (JCWEBDEN.COM)                         *
 *    <http://www.jcwebden.com/online/legal/copyright.php>                    *
 *                                                                            *
 ******************************************************************************
 *    The Pacer Edition CMS End User License Agreement (EULA)                 * 
 *    <http://www.jcwebden.com/online/legal/eula-license.php>                 *
 *                                                                            * 
 ******************************************************************************
 * The Pacer Edition CMS is provided as open source/commercial software.      *
 * Providing commercial applications within the scope of The Pacer            *
 * Edition CMS software; You can redistribute The Pacer Edition CMS           *
 * and/or modify it under the terms of The Pacer Edition Copyright License    *
 * as published by The Pacer Edition CMS though the scope of modifing         *
 * and/or providing commercial and/or open source add-ons, documentation,     *
 * modifications, patches, updates, is bound by the copyright terms layed     *
 * out at <http://www.jcwebden.com/online/legal/copyright.php>                *
 *                                                                            *
 * The Pacer Edition CMS is distributed ASIS in the hope that it will         *
 * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty     *
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                    *
 *                                                                            *
 * Copyright (C) 2009, JCWEBDEN.COM (DARKSTAR INCORPORATED)                   *
 *                                                                            *
 ******************************************************************************
 *           Started by Chio Maisriml, Ported for Pacer by JCWEBDEN           *
 ******************************************************************************
 */

function addcomment (cid, t) {
	if (cid != lastcommentid) {
		document.getElementById("lastcomment").innerHTML  = t;
	} 
} 

function showcommenturl (t2, t1) {
	theurl = t1 + t2;
	window.open(theurl); 
}

function resizeframe (h) {
	h = 10 + h;
	hpx = ''+h+'px';
	document.getElementById("extrasager").style.height = hpx;
}

function makesmaller () {
	document.getElementById("extrasager").style.height = "30px";
}

function vote(auth,url,vid,vfor,msg_w,header_w,msg_s,header_s,msg_l,header_l,rate,ok){
	if (auth){
		jQuery(rate).unbind('onclick');
		rate.onclick='';
		var c = jQuery(rate).find('div span.count');
		c.html('&nbsp;<input type="text" size="2" maxlenght="2" value="1" readonly="readonly" style="display:none"/>');
		var a = jQuery(rate).find('div span.vote');
		a.html('<span onclick="javascript:vote_rate_updater('+auth+',\''+url+'\',\''+vid+'\',\''+vfor+'\',\''+msg_w+'\',\''+header_w+'\',\''+msg_s+'\',\''+header_s+'\',\''+msg_l+'\',\''+header_l+'\',1)">'+ok+'</span>');
		c.focus();
	} else {
		vote_rate_msg(header_l,msg_l,'warning',true)
	}

}
function rate(auth,url,vid,vfor,msg_w,header_w,msg_s,header_s,msg_l,header_l,rate,ok){
	if (!jQuery(rate).hasClass('no_vote')){													
		if (auth){
			jQuery(rate).unbind('onclick');
			rate.onclick='';
			var c = jQuery(rate).find('div span.count');
			c.html('<input type="text" size="1" maxlength="1" value="" title="0..9"/>');
			var a = jQuery(rate).find('div span.rate');
			a.html('<span onclick="javascript:var v=jQuery(this).parent().parent().find(\'span.count input\').attr(\'value\');vote_rate_updater('+auth+',\''+url+'\',\''+vid+'\',\''+vfor+'\',\''+msg_w+'\',\''+header_w+'\',\''+msg_s+'\',\''+header_s+'\',\''+msg_l+'\',\''+header_l+'\',v)">'+ok+'</span>');
			jQuery(c).find('input').focus();
		} else {
			vote_rate_msg(header_l,msg_l,'warning',true)
		}
	}
}
function rate_stars(url,vfor,rate){
	if (jQuery(rate).hasClass('no_vote'))													
		return false;
	
	var sp = jQuery(rate).parent().parent().parent().find('span.loading');
	var ul = jQuery(rate).parent().parent();
	ul.addClass('unhideme');
	sp.addClass('hideme');
	ul.hide();
	sp.show();

	var id=jQuery(rate).attr('id');
	var elem = id.split(",");
	vote_rate_updater(url,vfor,elem[1]);
}
function vote_rate_updater(url,vfor,vra){
	if(!vra) vra=0;
	jQuery.post(url,{vfor: vfor, vra: vra}, function(data){
		if (data == '-3') { // Not Auth
			vote_rate_msg(vote_header_l,vote_msg_l,'error',false);
		} else {
			if (data == '-2') { // Wait 60secs before next vote...
				vote_rate_msg(vote_header_a,vote_msg_a,'warning',false);
			} else {
				if (data == '-1') { // Vote already casted
					vote_rate_msg(vote_header_w,vote_msg_w,'warning',false);
				} else { // Success
					vote_rate_msg(vote_header_s,vote_msg_s,'success',true);
				}
			}
		}
	});	
}
function vote_rate_msg(header,msg,theme,reloadme){
	if (jQuery.jGrowl) {
		jQuery.jGrowl(msg, {theme:theme,header: header, sticky: false, close: function(e,m,o) {
			if (reloadme) self.location.reload();
			else {
				jQuery('div.mod_topic_ratebox_stars span.loading').each(function(){
					if (jQuery(this).hasClass('hideme')){
						jQuery(this).removeClass('hideme');
						jQuery(this).hide();
					}
				});
				jQuery('ul.mod_topic_ratebox_stars').each(function(){
					if (jQuery(this).hasClass('unhideme')){
						jQuery(this).removeClass('unhideme');
						jQuery(this).show();
					}
				});

			}
		}}
		);	
	} else {
		alert(header+'\n'+msg);
		self.location.reload();
	}
}
jQuery(document).ready(function() {
jQuery('ul.mod_topic_ratebox_stars li a').each(function(){
	if (!jQuery(this).hasClass('no_vote')){
	jQuery(this).hover(
      function () {
		var id=jQuery(this).attr('id');
		var elem = id.split(",");
		for(var x=1;x<=elem[1];x++){
			jQuery('ul.mod_topic_ratebox_stars li a[id="'+elem[0]+','+x+'"]').addClass('star-red');
		};
	  }, 
      function () {
		var id=jQuery(this).attr('id');
		var elem = id.split(",");
		for(var x=1;x<=5;x++){
			jQuery('ul.mod_topic_ratebox_stars li a[id="'+elem[0]+','+x+'"]').removeClass('star-red');
		};
      }
	);
	} else {
		jQuery(this).removeAttr('href');
		jQuery(this).click(function(){
			return false;
		});
	}
});
});