﻿var curPos = 0;
var paused = false;
var timer = 0;
var saveCookie = false;
var imgcid = 0;
var imgtcnt;

function isINT(x){
	var y = parseInt(s);
	if (isNaN(y))
		return false;
	else
		return x;
}

function intCheck(x, y){
	if (isINT(x))
		return x;
	else return y;
}


function writeCookie(a_id, a_value){
	if (saveCookie)
		$.cookie(a_id, a_value, {expires: 365});
}

function setImgNav(){
		if (imgcid <= 0){
			$("#imgprebtn").html('<img class="imgprebtnd" style="width: 21px; height: 94px; border: 0px;" src="imgv2/img_1x1_empty.gif"/>');
		} else {
			$("#imgprebtn").html('<a href="javascript:;"><img class="imgprebtn" style="width: 21px; height: 94px; border: 0px;" src="imgv2/img_1x1_empty.gif"/></a></td>');
			
		}
		if ((imgcid + 7) >= imgtcnt){
			$("#imgnxtbtn").html('<img class="imgnxtbtnd" style="width: 21px; height: 94px; border: 0px;" src="imgv2/img_1x1_empty.gif"/>');
		} else {
			$("#imgnxtbtn").html('<a href="javascript:;"><img class="imgnxtbtn" style="width: 21px; height: 94px; border: 0px;" src="imgv2/img_1x1_empty.gif"/></a></td>');
		}
}


$('.b_l').ready(function(){
	$('.b_a_l').fadeOut(500, function(){
		$('.b_l').attr('class', '');
		$('.b_a_l').html('');		
		$('#art_abl p a').attr('target', '_blank');
	});
});

$(document).ready(function(){
	$('#sr_coment').click(function(){
		$('.bm_viewwnd_opt_comment').slideToggle(200);
	});
	
	$('#sr_moreonit').click(function(){
		$('.bm_moreonit').slideToggle(200);
	});
	

	$('.imgthm img').live('mouseover', function(){
		var imgx = this;
			$('#imgview_morecom').slideToggle(300, function(){
			$('#imgview_morecom div').html($(imgx).attr('altt'));
			$('#imgview_morecom div').fadeIn(500);
});
	});

	
	$('.imgthm img').live('mouseout', function(){
		$('#imgview_morecom').slideToggle(500);
		$('#imgview_morecom div').fadeOut(300);
	});

	setImgNav();
	
	$('#imgprebtn a').live('click', function(){
		imgcid = imgcid-7;
		if (imgcid <=0)
			imgcid = 0;
		fPage = 'pages.inc/moreimg.callback.php?callback=true&ls='+imgcid+'&id='+randomString();
		$.get(fPage, function(data){
			$('#rj_imgcont').html(data);
			setImgNav();
		});
	});
	
	
	$('#imgnxtbtn a').live('click', function(){
		imgcid = imgcid+7;
		if (imgcid >= imgtcnt)
			imgcid = imgtcnt;
		fPage = 'pages.inc/moreimg.callback.php?callback=true&ls='+imgcid+'&id='+randomString();
		$.get(fPage, function(data){
			$('#rj_imgcont').html(data);			
			setImgNav();
		});
	});

	
	$('.bm_head p a.toggle').click(function(){
		if ($(this).parents('div').next().children().is(":hidden")){		
			$(this).parents('div').next().children().fadeIn(1000);
			writeCookie($(this).attr('id'), true);
		} else {
			$(this).parents('div').next().children().fadeOut(1000);
			writeCookie($(this).attr('id'), false);
		}
		$(this).parents('div').next().slideToggle(1000);
	});
	
	$('#art_mailit').click(function(){
		$('.bm_viewwnd_opt_email').slideToggle(500);
	});
	
	$('#emailit_cancel').click(function(){
		$('.bm_viewwnd_opt_email').slideToggle(500);
	});
	
	$('.commentbox #showspam').click(function(){
		se = $(this);
		spam = $('.'+$(this).attr('dID'));
		if ($(spam).is(":hidden")){
			$(spam).slideToggle(500, function (){
				se.html('Marked as spam, hide it');
			});
		} else {
			$(spam).slideToggle(500, function (){
				se.html('Marked as spam, show it');
			});
		}
	});

	$('.g_poll a.alink').click(function(){
		doVoteShowHide();
	});

	$('#pollvote').submit(function(){
		var voted = $('#voted').val();
		fPage = 'pages.inc/vote.callback.php?voted='+voted+'&id='+randomString();	
		$.post(fPage, $('#pollvote').serialize(), function(data){
			if (data == 'no')
				alert('Sorry, but you cannot vote more than one at a session.\n\nमाफ गर्नुहोला, तर तपाईले एक चोटि भन्दा बढि भोट गर्न एक समयमा पाउनु हुदैन ।');
			else if (data == 'empty')
				alert('Please select an answer to vote.\n\nकृपया, भोट गर्न कुनै उत्तर छान्नुहोस् ।');
			else {
				$('#voted').val('1');
				doVoteAction();
			}
		});		
		return false;
	});
		
	$('#vpte').click(function(){
		alert($(pollvote.poll.value));
	});
	

	$('.hreiq').click(function(){
		$('#iqel').slideToggle('slow');
	});


	$('#getfeedback').click(function(){
		if ($('#feedbackarea').is(":hidden")){
		} else {
			$("#bm_h").html('&#2346;&#2366;&#2336;&#2325; &#2346;&#2381;&#2352;&#2340;&#2367;&#2325;&#2371;&#2351;&#2366;');
			$('#mailto').val('gautamrajan@hotmail.com');
			$('#mailtox').val('');
		}
		$('#feedbackarea').slideToggle(500);
	});
	
	$('#mailfeeds a').click(function(){
		$("#bm_h").html('&#2346;&#2366;&#2336;&#2325; &#2346;&#2381;&#2352;&#2340;&#2367;&#2325;&#2371;&#2351;&#2366; '+$(this).attr('authorname')+" को लागी");
		$('#mailto').val($(this).attr('addr'));
		$("#mailtox").val($("#mailto").val());
		
		if ($('#feedbackarea').is(":hidden")){
			$('#feedbackarea').slideToggle(500);
		}
	});
	
	$("#cancelfeed").click(function(){
		feedboxclose();
	});
	
	$('#feed_back').submit(function(){
		fPage = 'pages.inc/sendfeed.callback.php?id='+randomString();
		$.post(fPage, $('#feed_back').serialize(), function(data){
			alert(data+'\n\nThank you for your message');
			feedboxclose();
		});		
		return false;
	});

		
	$(".ph_next").click(function(){
		getNextImg(false);
	});
	
	$(".ph_pre").click(function(){
		getPrevImg(false);
	});

	$(".ph_play").click(function(){
		imgpause(true);
	});

	
	if (typeof allowPhoto != 'undefined'){
		timer = setInterval(function(){ 
			getNextImg();
		}, 5000);
	
		onLoadImg();
	}
	
	// guest book
	$('.gb_write').click(function(){
		if ($('#gb_sign').is(":hidden")){
			$('#gb_sign').slideToggle(300);
		} else {
$(':input','#gsign') 
 .not(':button, :submit, :reset, :hidden, :checkbox') 
 .val('') 
 .removeAttr('checked') 
 .removeAttr('selected'); 

			$('#gb_sign').slideToggle(300);
		}
	});
	
	$('.gb_head a').live("click", function(){
		var gb_d = $(this).attr('rel');
		$('#'+gb_d).slideToggle(300);
	});	
		
	$('#gbcancel').click(function(){
$(':input','#gsign') 
 .not(':button, :submit, :reset, :hidden, :checkbox') 
 .val('') 
 .removeAttr('checked') 
 .removeAttr('selected'); 

		$('#gb_sign').slideToggle(300);
	});
	
	$('#gsign').submit(function(){
		fPage = 'pages.inc/gb_submit.callback.php?id='+randomString();
		$.post(fPage, $('#gsign').serialize(), function(data){
//			alert(data);
			if (data != 'no'){
$(':input','#gsign') 
 .not(':button, :submit, :reset, :hidden')
 .val('') 
 .removeAttr('checked') 
 .removeAttr('selected'); 

				$('#gb_sign').slideToggle(300, function(){
					var toAdddiv = $('<div>').html(data);
					$(toAdddiv).hide();
					$('#gb_container').prepend(toAdddiv);
					$(toAdddiv).slideToggle(400);
				});
			} else {
				alert('You have already signed to the guestbook or you have leaved the check box unchecked. Please, refresh your browser and check the checkbox before you sign.\r\nThank you');
			}
		});		
		return false;
	});
	
	$('.gb_more').click(function(){			
		var img = new Image();
		$(img).load(function (){
			$('#gb_xe').fadeOut(400, function(){
				$(".gb_spif").fadeIn(400, function(){
				
				fPage = 'pages.inc/gb_more.callback.php?callback=true&from='+(gb_current)+'&limit=10&id='+randomString();
					$.get(fPage, function(data){
			var toAdddiv = $('<div>').html(data);
			$(toAdddiv).hide();
			$('#gb_container').append(toAdddiv);
			$(toAdddiv).slideToggle(800);
						$(".gb_spif").fadeOut(400, function(){
							$('#gb_xe').fadeIn(400);
						});
					});
				
				});
			});
		}).attr('src', 'imgv2/spif16x16.gif');

	});


	$('#art_chn_font_size a').click(function(){
		var reeer = $(this).attr('rel');
		if (reeer == 'f10')
			fs = '10pt';
		else if (reeer == 'f14')
			fs = '14pt';
		else if (reeer == 'f18')
			fs = '18pt';
		else 
			fs = '12pt';
		$('#art_abl').css({'font-size':fs});
	});
	
	$('#art_chn_font_sizex a').click(function(){
		var reeer = $(this).attr('rel');
		if (reeer == 'f10')
			fs = '10pt';
		else if (reeer == 'f14')
			fs = '14pt';
		else if (reeer == 'f18')
			fs = '18pt';
		else 
			fs = '12pt';
		$('#view_wndx').css({'font-size':fs});
	});

});


function imgpause(loadnext){
	img = $(".ph_play img");
	if (paused){
		timer = setInterval(function(){ 
			getNextImg();
		}, 5000);
		if (loadnext)
			getNextImg();

		$(img).attr('src', 'imgv2/img_ph_plu.gif');

	} else {
		clearInterval(timer);
		$(img).attr('src', 'imgv2/img_ph_ply.gif');
	}
	paused = paused == false?true:false;
}

function onLoadImg(){
		var img = new Image();
		$(img).load(function (){
			$(this).hide();
			$(img).attr('class', 'img_photo');
			
			var linkPhoto = $('<a>');
			$(linkPhoto).attr('href', ph_link_arr[curPos]);
			$(linkPhoto).append(img);
			$('.ph_photo').append(linkPhoto);
			
			$(img).fadeIn(400);
			$('.ph_comment div').fadeOut(400, function(){
				$('.ph_comment').html('');
				var toAdd = $('<div>').html(ph_com_arr[curPos]);
				$(toAdd).hide();
				$('.ph_comment').append(toAdd);
				$(toAdd).fadeIn(400);
			});
			$('.numofimg').hide();
			$('.numofimg').html((curPos+1)+' of '+ph_array.length);
			$('.numofimg').fadeIn(400);
		}).attr('src', ph_array[curPos]);
}

function getNextImg(){
t = false;
		if (!paused) {
			t = true;
			imgpause(false);
		}
		curPos++;
		if (curPos >= ph_array.length)
			curPos = 0;
		var img = new Image();
		$(img).load(function (){
			$(this).hide();
			$('.img_photo').fadeOut(400, function(){
				$('.ph_photo').html('');
				$(img).attr('class', 'img_photo');
//				$('.ph_photo').append(img);
			var linkPhoto = $('<a>');
			$(linkPhoto).attr('href', ph_link_arr[curPos]);
			$(linkPhoto).append(img);
			$('.ph_photo').append(linkPhoto);

				$(img).fadeIn(400);
			});
			$('.ph_comment div').fadeOut(400, function(){
				$('.ph_comment').html('');
				var toAdd = $('<div>').html(ph_com_arr[curPos]);
				$(toAdd).hide();
				$('.ph_comment').append(toAdd);
				$(toAdd).fadeIn(400);
			});
			$('.numofimg').fadeOut(200, function(){
				$('.numofimg').html((curPos+1)+' of '+ph_array.length);
				$('.numofimg').fadeIn(200);
			});

		}).attr('src', ph_array[curPos]);
		
	if (t)
		imgpause(false);
}

function getPrevImg(){
t = false;
		if (!paused) {
			t = true;
			imgpause(false);
		}

		if (curPos <= 0)
			curPos = ph_array.length;

		curPos--;
		var img = new Image();
		$(img).load(function (){
			$(this).hide();
			$('.img_photo').fadeOut(400, function(){
				$('.ph_photo').html('');
				$(img).attr('class', 'img_photo');
//				$('.ph_photo').append(img);
			var linkPhoto = $('<a>');
			$(linkPhoto).attr('href', ph_link_arr[curPos]);
			$(linkPhoto).append(img);
			$('.ph_photo').append(linkPhoto);

				$(img).fadeIn(400);
			});
			$('.ph_comment div').fadeOut(400, function(){
				$('.ph_comment').html('');
				var toAdd = $('<div>').html(ph_com_arr[curPos]);
				$(toAdd).hide();
				$('.ph_comment').append(toAdd);
				$(toAdd).fadeIn(400);
			});
			$('.numofimg').fadeOut(200, function(){
				$('.numofimg').html((curPos+1)+' of '+ph_array.length);
				$('.numofimg').fadeIn(200);
			});
		}).attr('src', ph_array[curPos]);
	if (t)
		imgpause(false);

}


function feedboxclose(){
		if ($('#feedbackarea').is(":hidden")){
		} else {
			$("#bm_h").html('&#2346;&#2366;&#2336;&#2325; &#2346;&#2381;&#2352;&#2340;&#2367;&#2325;&#2371;&#2351;&#2366;');
			$('#mailto').val('gautamrajan@hotmail.com');
			$('#mailtox').val('');
		}
		$('#feedbackarea').slideToggle(500);

}

function goback(){
	window.history.back();
}

function randomString() {
	var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
	var string_length = 8;
	var randomstring = '';
	for (var i=0; i<string_length; i++) {
		var rnum = Math.floor(Math.random() * chars.length);
		randomstring += chars.substring(rnum,rnum+1);
	}
	return randomstring;
}

function doVoteShowHide(){
		se = $('.g_poll a.alink');
		elp = document.getElementById("pollresult");
		el = $(elp).children();
		
		if ($(el).html() != ''){
			$(elp).slideToggle(800, function(){
				$(el).html('');
			});
			$("#mcnt").fadeOut(800, function(){
				$(se).html('View Result');
				$(this).fadeIn(800);
			});
		} else {
			$(el).html('');


		$('#mcnt').fadeOut(300, function(){
			var img = new Image();
			$(img).load(function (){
				$(".spif").fadeIn(800)				
				// get twits
				fPath = 'pages.inc/pollresult.callback.php?id='+randomString();
				$.get(fPath, function(data){
					var toAdddiv = $('<div>').html(data);
					$(el).append(toAdddiv);
					$(elp).slideToggle(800);
					$(".spif").fadeOut(500, function(){	
						$(se).html('Hide Result');
						$("#mcnt").fadeIn(600);
					});
				});
			}).attr('src', 'imgv2/spif16x16.gif');
		});
	}
}



function doVoteAction(){
	se = $('.g_poll a.alink');
	elp = document.getElementById("pollresult");
	el = $(elp).children();
	elc = $(el).children();
		
	if ($(el).html() == ''){
		doVoteShowHide();			
	} else {
		$(el).height($(elp).height());
			
		$('#mcnt').fadeOut(300, function(){
			var img = new Image();
			$(img).load(function (){
				$(".spif").fadeIn(800)				
				fPath = 'pages.inc/pollresult.callback.php?id='+randomString();
				$(elc).fadeOut(400, function(){
					$.get(fPath, function(data){
						$(elc).html(data);
						$(elc).fadeIn(800);
						$(".spif").fadeOut(500, function(){	
							$(se).html('Hide Result');
							$("#mcnt").fadeIn(600);
						});
					});
				});
			}).attr('src', 'imgv2/spif16x16.gif');
		});
	}
}

function doVoteShowHidex(){
		se = $('.g_poll a.alink');
		el = document.getElementById("pollresult");
		el = $(el).children();
		
		if ($(el).html() != ''){
			$(el).children().slideToggle(800, function(){
				$(el).html('');
			});
			$("#mcnt").fadeOut(800, function(){
				$(se).html('View Result');
				$(this).fadeIn(800);
			});
		} else {
			$(el).html('');


		$('#mcnt').fadeOut(300, function(){
			var img = new Image();
			$(img).load(function (){
				$(".spif").fadeIn(800)				
				// get twits
				fPath = 'pages.inc/pollresult.callback.php?id='+randomString();
				$.get(fPath, function(data){
					var toAdddiv = $('<div>').html(data);
					$(toAdddiv).hide();
					$(el).append(toAdddiv);
					$(toAdddiv).slideToggle(800);
					$(".spif").fadeOut(500, function(){	
						$(se).html('Hide Result');
						$("#mcnt").fadeIn(600);
					});
				});
			}).attr('src', 'imgv2/spif16x16.gif');
		});
		}

}

function emoticon(text) {
	var txtarea = document.gsign.msg;
	text = ' ' + text + ' ';
	if (txtarea.createTextRange && txtarea.caretPos) {
		var caretPos = txtarea.caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? caretPos.text + text + ' ' : caretPos.text + text;
		txtarea.focus();
	} else {
		txtarea.value  += text;
		txtarea.focus();
	}
}

