function checkAll(field){
	for (i = 0, j = field.length; i < j; i++) field[i].checked = true;
}

function uncheckAll(field){
	for (i = 0, j = field.length; i < j; i++) field[i].checked = false;
}

// Select checkbox after changing credit value
function premium_check_select(num){
	$("#s_" + num)[0].checked = true;
}

function quotemessage(){
	document.puf.poruka.value = quote + document.puf.poruka.value;
	quote = '';
	return false;
}

// Dohvati zupanije za odredenu drzavu
function get_zupanije(country_id, select_type){

	$.get("common/live/get_zupanije.php?id=" + country_id + "&type=" + select_type,
		function(data){
			if(data != 'false'){
				$("#zupanije").html(data);
			}
			
		}
	);
}

jQuery.fn.extend({
	every: function(interval, label, fn, count) {
		return this.each(function() {
			jQuery.timer.add(this, interval, label, fn, count);
		});
	},
	once: function(interval, label, fn) {
		return this.each(function() {
			jQuery.timer.add(this, interval, label, fn, 1);
		});
	},
	stop:  function(label, fn) {
		return this.each(function() {
			jQuery.timer.remove(this, label, fn);
		});
	}
});

jQuery.extend({
	timer: {
		guid: 1,
		global: {},
		add: function(element, interval, label, fn, count) {
			var counter = 0;
			count = count || 0;
			
			if (jQuery.isFunction(label)) {
				count = fn;
				fn = label;
				label = interval;
			}
			
			if (!element.$timers) 
				element.$timers = {};
			
			if (!element.$timers[label])
				element.$timers[label] = {};
			
			fn.$timerID = fn.$timerID || this.guid++;
			
			var handler = function() {
				if ((++counter > count && count !== 0) || fn.call(element, counter) === false)
					return jQuery.timer.remove(element, label, fn);
			};
			
			handler.$timerID = fn.$timerID;
			
			if (!element.$timers[label][fn.$timerID]) 
				element.$timers[label][fn.$timerID] = window.setInterval(handler,interval);
			
			if ( !this.global[label] )
				this.global[label] = [];
			this.global[label].push( element );
			
		},
		remove: function(element, label, fn) {
			var timers = element.$timers, ret;
			
			if ( timers ) {
				
				if (!label) {
					for ( label in timers )
						this.remove(element, label, fn);
				} else if ( timers[label] ) {
					if ( fn ) {
						if ( fn.$timerID ) {
							window.clearInterval(timers[label][fn.$timerID]);
							delete timers[label][fn.$timerID];
						}
					} else {
						for ( var fn in timers[label] ) {
							window.clearInterval(timers[label][fn]);
							delete timers[label][fn];
						}
					}
					
					for ( ret in timers[label] ) break;
					if ( !ret ) {
						ret = null;
						delete timers[label];
					}
				}
				
				for ( ret in timers ) break;
				if ( !ret ) 
					element.$timers = null;
			}
		}
	}
});

$(document).ready(function(){
	
	// Dohvati novi broj poruka ako je korisnik ulogiran i online VIP clanove
	if(typeof index != "undefined"){
	
		$("#new_messages").every(20000, function() {
							
			$.get("common/live/user_messages.php",
				function(data){
				
					$("#new_messages span.msg_no_notice").html(data);
					
					if(data == "0"){ location.href='index.php'; }
					
					if(data == 'Pošalji poruku!'){
						$("#new_messages").attr("href", "messages_send.php");
					}
					else {
						$("#new_messages").attr("href", "messages.php");
					}
					
					if(last_imsg != data){
						if(msg_sound_status == 'on') PlayFlashMovie('msg_sound');
						last_imsg = data;
					}
					
				}
			);
						
		});
		
		$("#vip_online").every(25000, function() {
						
			$("#vip_online").load("common/live/user_vip_online.php");
						
		});
		
		$("#last_profiles").every(35000, function() {
						
			$("#last_profiles").load("common/live/user_last_profiles.php");	
						
		});
		
	}
	
	if(typeof index_public != "undefined"){
		/*
		Copyright (c) Copyright (c) 2007, Carl S. Yestrau All rights reserved.
		Code licensed under the BSD License: http://www.featureblend.com/license.txt
		Version: 1.0.2
		*/
		var FlashDetect = new function(){
			var self = this;
			self.installed = false;
			self.major = -1;
			self.minor = -1;
			self.revision = -1;
			self.revisionStr = "";
			self.activeXVersion = "";
			var activeXDetectRules = [
				{
					"name":"ShockwaveFlash.ShockwaveFlash.7",
					"version":function(obj){
						return getActiveXVersion(obj);
					}
				},
				{
					"name":"ShockwaveFlash.ShockwaveFlash.6",
					"version":function(obj){
						var version = "6,0,21";
						try{
							obj.AllowScriptAccess = "always";
							version = getActiveXVersion(obj);
						}catch(err){}
						return version;
					}
				},
				{
					"name":"ShockwaveFlash.ShockwaveFlash",
					"version":function(obj){
						return getActiveXVersion(obj);
					}
				}
			];
			var getActiveXVersion = function(activeXObj){
				var version = -1;
				try{
					version = activeXObj.GetVariable("$version");
				}catch(err){}
				return version;
			};
			var getActiveXObject = function(name){
				var obj = -1;
				try{
					obj = new ActiveXObject(name);
				}catch(err){}
				return obj;
			};
			var parseActiveXVersion = function(str){
				var versionArray = str.split(",");//replace with regex
				return {
					"major":parseInt(versionArray[0].split(" ")[1], 10),
					"minor":parseInt(versionArray[1], 10),
					"revision":parseInt(versionArray[2], 10),
					"revisionStr":versionArray[2]
				};
			};
			var parseRevisionStrToInt = function(str){
				return parseInt(str.replace(/[a-zA-Z]/g, ""), 10) || self.revision;
			};
			self.majorAtLeast = function(version){
				return self.major >= version;
			};
			self.FlashDetect = function(){
				if(navigator.plugins && navigator.plugins.length>0){
					var type = 'application/x-shockwave-flash';
					var mimeTypes = navigator.mimeTypes;
					if(mimeTypes && mimeTypes[type] && mimeTypes[type].enabledPlugin && mimeTypes[type].enabledPlugin.description){
						var desc = mimeTypes[type].enabledPlugin.description;
						var descParts = desc.split(' ');//replace with regex
						var majorMinor = descParts[2].split('.');
						self.major = parseInt(majorMinor[0], 10);
						self.minor = parseInt(majorMinor[1], 10); 
						self.revisionStr = descParts[3];
						self.revision = parseRevisionStrToInt(self.revisionStr);
						self.installed = true;
					}
				}else if(navigator.appVersion.indexOf("Mac")==-1 && window.execScript){
					var version = -1;
					for(var i=0; i<activeXDetectRules.length && version==-1; i++){
						var obj = getActiveXObject(activeXDetectRules[i].name);
						if(typeof obj == "object"){
							self.installed = true;
							version = activeXDetectRules[i].version(obj);
							if(version!=-1){
								var versionObj = parseActiveXVersion(version);
								self.major = versionObj.major;
								self.minor = versionObj.minor; 
								self.revision = versionObj.revision;
								self.revisionStr = versionObj.revisionStr;
								self.activeXVersion = version;
							}
						}
					}
				}
			}();
		};
		FlashDetect.release = "1.0.2";
		
		if(FlashDetect.installed){
			var ci_fv = FlashDetect.major; 
			ci_fv += ' ' + FlashDetect.minor;  
			ci_fv += ' ' + FlashDetect.revision;
			ci_fv += ' ' + FlashDetect.revisionStr;
			document.getElementById('CI_F').value = ci_fv;
		}
		
		if( self.screen ) {
			N = screen.pixelDepth
			? Math.pow( 2, screen.pixelDepth )
			: Math.pow( 2, screen.colorDepth )
		}
		else { N = 0 }
		document.getElementById('CI_SD').value = (N/1024/1024);
	}
	
	// Sakrij Iskrica info
	$("a.close_info").click(function(){
	
		this.idParts = this.id.split("_");
		$("div#infobox_" + this.idParts[1]).slideUp("normal");
		
		var cookie_date = new Date();
		cookie_date.setDate(cookie_date.getDate()+30);
		
		document.cookie = "info_"+this.idParts[1]+"=1; expires="+cookie_date.toGMTString()+"; path=/; domain="+cookie_domain;
		
		return false;
	});
	
	// Sakrij / prikazi nagradnu igru
	$("#prize_game").click(function(){
	
		var has_class = $(this).hasClass("prize_game_min");

		if(has_class){
		
			$(this).removeClass("prize_game_min");
		
			var cookie_date = new Date();
			cookie_date.setDate(cookie_date.getDate()+1);
			
			document.cookie = "prize_min=1; expires="+cookie_date.toGMTString()+"; path=/; domain="+cookie_domain;

			$(this).addClass("prize_game_max");

		}
		else {
			
			$(this).removeClass("prize_game_max");
			
			var cookie_date = new Date();
			cookie_date.setDate(cookie_date.getDate()-30);
			
			document.cookie = "prize_min=0; expires="+cookie_date.toGMTString()+"; path=/; domain="+cookie_domain;

			$(this).addClass("prize_game_min");
		
		}
	
		$("#prize_game_container").toggle();
		
		return false;
	});
	
	// Sakrij/prikazi pokloni premium paket
	$("#sh_give_premium").click(function(){
		$("#premium_give").toggle();
		return false;
	});
	
	// Sakrij/prikazi opciju slanja poklona
	$(".collect_sh_user").click(function(){
	
		this.idParts = this.id.split("_");
	
		$("#u_" + this.idParts[1]).toggle();
		return false;
	});
	
	// input selector na porukama
	$("#input_msg_check").click(function(){
	 	if(document.getElementById('input_msg_check').checked)checkAll(document.ofo);
	 	else uncheckAll(document.ofo);
	});
	
});

// Premium tip links
function premium_tip(id){

	$.get("premium_data/tips.php?live=" + id,
		function(data){
			$("div#premium_tip").html(data);
		}
	);
	
	return false;
}

/* Greybox Redux
 * Required: http://jquery.com/
 * Written by: John Resig
 * Based on code by: 4mir Salihefendic (http://amix.dk)
 * License: LGPL (read more in LGPL.txt)
 */

var GB_DONE = false;
var GB_HEIGHT = 400;
var GB_WIDTH = 400;

function GB_show(caption, url, height, width) {
  GB_HEIGHT = height || 400;
  GB_WIDTH = width || 400;
  if(!GB_DONE) {
    $(document.body)
      .append("<div id='GB_overlay'></div><div id='GB_window'><div id='GB_caption'></div>"
        + "<img src='images/close.gif' alt='Close window'/></div>");
    $("#GB_window img").click(GB_hide);
    $("#GB_overlay").click(GB_hide);
    $(window).resize(GB_position);
    GB_DONE = true;
  }

  $("#GB_frame").remove();
  $("#GB_window").append("<iframe id='GB_frame' src='"+url+"'></iframe>");

  $("#GB_caption").html(caption);
  $("#GB_overlay").show();
  GB_position();

  if(GB_ANIMATION)
    $("#GB_window").slideDown("slow");
  else
    $("#GB_window").show();
}

function GB_hide() {
  $("#GB_window,#GB_overlay").hide();
}

function GB_position() {
  var de = document.documentElement;
  var w = self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
  $("#GB_window").css({width:GB_WIDTH+"px",height:GB_HEIGHT+"px",
    left: ((w - GB_WIDTH)/2)+"px" });
  $("#GB_frame").css("height",GB_HEIGHT - 32 +"px");
} 

/***********************************/

function getFlashMovieObject(movieName){

	if (window.document[movieName]) {
		return window.document[movieName];
	}
	if (navigator.appName.indexOf("Microsoft Internet") == -1){
	    if (document.embeds && document.embeds[movieName]){ return document.embeds[movieName]; }
	}
	else {
		return document.getElementById(movieName);
	}
}

function PlayFlashMovie(movie){
	getFlashMovieObject('msg_sound').Play();
}


