﻿var picChInt = 0;
var imageArr = new Array();
var TR = {
	'v':'1.5.1'
}

TR.defaults = {
	defaultMenu : 'A'
}

TR.menu = {
	crrClass : TR.defaults.defaultMenu,
	setTo : function(c){
		$("#menuDivBox").removeClass("menuPanel"+this.crrClass);
		$("#menuDivBox").addClass("menuPanel"+c);
		this.crrClass = c;
	}
}

TR.ajaxBox = {
	openBox : function(f){
		$(".ajaxOverFlow").show();
		$("#ajaxTarget").html("<div id='ajaxLoad' style='margin:200px auto auto auto;'><img src='images/loader1.gif'/></div>");
		$("#ajaxTarget").ajaxError(function(event, request, settings){
			$("#ajaxLoad").fadeOut('slow',function(){
				$("#ajaxTarget").html("<div id='ajaxReady' style='display:none;'>Nevarēja ielādēt failu.</div>");
				$("#ajaxReady").fadeIn('slow');
			});
		});
		$.get(f,function(data){
			$("#ajaxLoad").fadeOut('slow',function(){
				$("#ajaxTarget").html("<div id='ajaxReady' style='display:none;'>"+data+"</div>");
				$("#ajaxReady").fadeIn('slow');
			});
		});
	},
	closeBox : function(){
		$(".ajaxOverFlow").hide();
		$("#ajaxTarget").html('');
	},
	openPoupUp : function(url){
		newwindow=window.open(url,'TRPopUp','height=400,width=600');
		if (window.focus) {newwindow.focus()}
	}
}

TR.ajax = {
	form : {
		newsLetter : {
			submit : function(){
				var m = $('#newsLetterRec').val();
				if(m=='Tavs e-pasts'){
					$('#NLStatusBox').html('<u style="color:red;cursor:pointer;" onclick="TR.ajaxBox.openBox(\'showMsgr.php?msg=E-pasts jānorāda obligāti!\');">Kļūda</u>');
				} else {
					$.post('post/newsLeterAdd.php',{mail:m},function(data){
						if(data['status']=='ok'){
							$('#newsLetterRec').val('Tavs e-pasts');
							setTimeout("$('#okFadeOut').fadeOut(function(){$('#okFadeOut').remove();});",3000);
						}
						$('#NLStatusBox').html(data['msg']);
					},'json');
				}
			}
		}
	}
}

TR.video = {
	currImage : 0,
	showImage : 1,
	videoHost : 'http://video.thereality.lv/',
	resetData : function(){
		this.currImage = 0;
		this.showImage = 1;
	},
	changeImg : function(i){
		arr = imageArr[i];
		$('#moviePic_'+i).attr('src',this.videoHost+'files/images/'+arr[this.currImage]);
		if(this.showImage<arr['m']){
			this.currImage++;
			this.showImage++;
		} else {
			this.currImage=0;
			this.showImage=1;
		}
	}
}

TR.animation = {
	favoriteVideoMove : function(type){
		if(type == 1 ){
			if(!animating) {
				if(pos<maxR){
					animating = true;
					$('#moveIcons').animate({marginLeft: pos+95}, 500, '', function(){ animating = false; }); 
					pos = pos+95;
				}
			}
		}else{
			if(!animating) {
				if(pos>maxL){
					animating = true
					$('#moveIcons').animate({marginLeft: pos-95}, 500, '', function(){ animating = false; }); 
					pos = pos-95;
				}
			}
		}
		//return true;
	},
	blogTitleMove : function(img,st){
		crrEl = TR.actions.blog.crrImage;
		if(img!=crrEl){
			if(img>=1&&img<=4){
				pos = ((parseInt(img)*200)-200);
				if(pos>=0&&pos<=600){
					pos = pos*(-1);
					$('#post_'+crrEl).animate({left: 0}, 500);
					$('#post_'+img).animate({left: -40}, 500);
					$('#bse').animate({marginTop: pos}, 500);
					if(st==true){
						stb = setTimeout('TR.actions.blog.imageSwitcher();',3000);
					} else {
						clearTimeout(stb);
						TR.actions.blog.crrImage = img;
						stb = setTimeout('TR.actions.blog.imageSwitcher();',5000);
					}
					//return true;
				} else {
					return 'Nav POS';
				}
			} else {
				return 'Nav IMG';
			}
		}
	},
	blogElementMove : function(pos){
		if(pos==(-1)||pos==(-352)){
			$('#blogElements').animate({marginLeft: pos}, 500);
			//return true;
		} else {
			return 'Nav POS';
		}
	}
}

TR.actions = {
	blog : {
		crrImage : 1,
		crrElementSet : 't',
		imageSwitcher : function(){
			if(this.crrImage == totalPics){
				open = 1;
			} else {
				open = this.crrImage+1;
			}
		TR.animation.blogTitleMove(open,true);
		this.crrImage = open;
		},
		elementSwich : function(el){
			if(el=='v'||el=='t'){
				if(el=='v'){
					p = -352;
				} else if(el=='t'){
					p = -1;
				}
				TR.animation.blogElementMove(p);
			} else {
				return 'Nav EL';
			}
		}
	},
	php_urlencode : function(str) {
		str = escape(str);
		return str.replace(/[*+\/@]|%20/g,
			function (s) {
				switch (s) {
					case "*": s = "%2A"; break;
					case "+": s = "%2B"; break;
					case "/": s = "%2F"; break;
					case "@": s = "%40"; break;
					case "%20": s = "+"; break;
				}
				return s;
			}
		);
	}
}
var text = new Array();
text[1] = 'Sūdīga';
text[2] = 'Ne visai';
text[3] = 'Normāla';
text[4] = 'Laba';
text[5] = 'Perfekta';
text[6] = '';
TR.rate = {
	stars : {
		change_to : function (t,s){
			if(israted != true){
				var sImgE = 'images/s_s.png';
				var sImgD = 'images/s_d.png';
				if(t){
					if(t==6){//0, lai JS nepārprot
						i1 = sImgD;
						i2 = sImgD;
						i3 = sImgD;
						i4 = sImgD;
						i5 = sImgD;
					}
					if(t==1){
						i1 = sImgE;
						i2 = sImgD;
						i3 = sImgD;
						i4 = sImgD;
						i5 = sImgD;
					}
					if(t==2){
						i1 = sImgE;
						i2 = sImgE;
						i3 = sImgD;
						i4 = sImgD;
						i5 = sImgD;
					}
					if(t==3){
						i1 = sImgE;
						i2 = sImgE;
						i3 = sImgE;
						i4 = sImgD;
						i5 = sImgD;
					}
					if(t==4){
						i1 = sImgE;
						i2 = sImgE;
						i3 = sImgE;
						i4 = sImgE;
						i5 = sImgD;
					}
					if(t==5){
						i1 = sImgE;
						i2 = sImgE;
						i3 = sImgE;
						i4 = sImgE;
						i5 = sImgE;
					}
					$("#rateImg1").attr({ 
						src: i1
					});
					$("#rateImg2").attr({ 
						src: i2
					});
					$("#rateImg3").attr({ 
						src: i3
					});
					$("#rateImg4").attr({ 
						src: i4
					});
					$("#rateImg5").attr({ 
						src: i5
					});
					if(!s&&s!=true){
						$("#rateText").html(text[t]);
					} else {
						$("#rateText").html('');
					}
				}
			}
		},
		postRating : function(a,b,t){
			if(israted != true){
				$.post("post/postRate.php", { type:a, id:b, value:t},
					function(data){
						$('#voterPlace').fadeOut(function(){
							$('#voterPlace').html(data);
							$('#voterPlace').fadeIn();
						});
						$('#rateText').html('');
				   });
				israted = true;
			} else {
				alert("Esi balsojis!")
			}
		}
	}
}

TR.poll = {
	crrOpt : 0,
	crrOptEl : 0,
    voteReturn : 0,
    voteComp : 0,
	changeOpt : function(i){
		nOpt = i;
		$("#osel_"+this.crrOptEl).removeClass('pollOptSel');
		this.crrOpt = nOpt;
		this.crrOptEl = i;
		$("#osel_"+nOpt).addClass('pollOptSel');
	},
    vote : function(){
        o = this.crrOpt;
        h = $('#formContent').height;
        $('#pollHolder').height(h);
        $.post("post/postPoll.php", { opt:o }, function(data){
            $('#formContent').fadeOut(function(){
                $('#formContent').html(data['html']);
                $('#formContent').fadeIn();
                $('#totalVotes').html(data['total']);
                TR.poll.voteReturn = data['v'].split("|");
                TR.poll.setVotes();
            });
        },'json');
    },
    setVotes : function(){
        for(i=0;i<=this.voteReturn.length-1;i++){
            var thisInfo = TR.poll.voteReturn[i].split('.');
            var id = thisInfo[0];
            var w = thisInfo[1];
            var p = thisInfo[2];
            if(w>104){
                var useId = 'optV1_'+id;
            } else {
                var useId = 'optV2_'+id;
            }
            $('#optSlide_'+id).animate({width: w}, 500);
            setTimeout("$('#"+useId+"').fadeIn();",550)
        }
    }
}

$(document).ready(function(){
	$("a#imageExpand").fancybox({
		'overlayShow':false,
		'zoomSpeedIn':600,
		'zoomSpeedOut':500,
		'easingIn':'easeOutBack',
		'easingOut':'easeInBack',
		'imageScale':true,
		'zoomOpacity':true,
		'overlayShow':false,
		'hideOnContentClick':true
	});
});

function DraugiemSay(title,url,titlePrefix){
 window.open(
  'http://www.draugiem.lv/say/ext/add.php?title=' + encodeURIComponent( title ) +
  '&link=' + encodeURIComponent( url ) +
  ( titlePrefix ? '&titlePrefix=' + encodeURIComponent( titlePrefix ) : '' ),
  '',
  'location=1,status=1,scrollbars=0,resizable=0,width=530,height=400'
 );
 return false;
}
