if(typeof(console)!='object') {console={log:function(){}}}
var webcam_watcher=null;


$(function() {
	$('.cake-sql-log tbody').hide();
	$('.cake-sql-log').click(function(){$('.cake-sql-log tbody').show();});
	
	$('.clickclear').each(function(){
        $(this).data('cc-i-val',this.value);
    }).click(function(){
        if(this.value==$(this).data('cc-i-val'))
            this.value='';
    });
	
	var shops = new rotator({
		container:$('#shop-rotator')
		,selected_content_class:'shop-rotator-content-selected'
		,delay:300
		,slideshow_delay:3000
	});
	var tm1=0;
	$('#mailing-user-signup-form').submit(function(){
		$.post($(this).attr('action'),$(this).serialize(),function(data){
			clearTimeout(tm1);
			$(data).modal();
			tm1=setTimeout(function(){$.modal.close();},5000);
			$('#simplemodal-overlay').click(function(){$.modal.close();});
		});
		return false;
	});
	$('#menu-item-6').click(function(){
		if(settings[0].Setting.value=='1') {
			ajax_loader();
			$.get("/branches/webcams",function(data){
				modal_reinit(data, 790,732);
				var first = $('#webcam-branch-input option:eq(1)').attr('value');
				$('#webcam-branch-input').val(first);
				$('#webcam-branch-input').trigger('change');
			});
		} else {
			alert('Funkcjonalność wyłączona. Przepraszamy.');
		}
	});
	$('#webcam-branch-input').live('change',function(){
		var branch_id = $(this).val();
		$('#webcam-r').html('');
		if(branch_id>0) {
			$.getJSON('/branches/get_info/'+branch_id,function(data){
				$("#webcam-l-info").html('');
				$('#webcamBranchTemplate').tmpl(data.Branch).appendTo("#webcam-l-info");
				if(webcam_watcher) {
					webcam_watcher.stop();
				}
				webcam_watcher = new webcams(data);
			});
		} else if(branch_id==-1) {
			$("#webcam-l-info").html('');
			
//			var iframe = $('<iframe></iframe>')
//				.attr('src','http://realrybnik.dyndns.org/AniCam4.htm')
//				.attr('width','525')
//				.attr('height','400')
//				.attr('id','webcams-iframe')
//				;
//			$('#webcam-r').html(iframe);
//			var w = $('#webcams-iframe').contents();
//			$(w).find('h2').remove();
			if(webcam_watcher) {
				webcam_watcher.stop();
			}
			webcam_watcher = new webcams(-1);
		}
	});
	$('#webcam-close').live('click',function(){
		if(webcam_watcher) {
			webcam_watcher.stop();
		}
		$.modal.close();
	});
	
	$('.cake-sql-log tbody').hide();
	$('.cake-sql-log').click(function(){$('.cake-sql-log tbody').show();});
});

function modal_reinit(data,w,h) {
	$('#loader1').replaceWith(data);
	var l = parseInt($(window).width());
	var t = parseInt($(window).height());
	l=l/2-w/2;
	try{
		if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
			l+=20;
		}
	} catch(ex) {}
	t=t/2-h/2;
	if(t<20)
		t=20;
	$('#simplemodal-container').animate({width:w+'px',height:h+'px',left:l+'px',top:t+'px'});
}

function ajax_loader() {
	$('<img id="loader1" src="/img/default/loader.gif" alt="..." />').modal();
	//$('<div id="lightbox"><div id="lightbox-wrap"><img id="loader1" src="/img/default/loader.gif" alt="..." /></div></div>').modal();
}

function webcams(data) {
	this.run=true;
	this.buffor_size=2;
	this.refresh_time=8000;
	this.firstrun=true;
	this.buffor=[];
	this.data=data;
	this.download();
	this.pic1=null;
	this.pic2=null;
	this.pic3=null;
	this.pic4=null;
}

webcams.prototype.download=function(){
	console.log('running');
	if(this.buffor.length<this.buffor_size) {
		var now = new Date();
		if(this.data==-1) {
			var cams = [
				{webcam:'http://79.187.55.242',i:0,now:now.getTime()}
				,{webcam:'http://czyzyny.dyndns.org',i:1,now:now.getTime()}
				,{webcam:'http://krareal.dyndns.org',i:2,now:now.getTime()}
				,{webcam:'http://sonarkatowice.dyndns.org',i:3,now:now.getTime()}
				,{webcam:'http://realrybnik.dyndns.org',i:4,now:now.getTime()}
				,{webcam:'http://hermesrybnik.dyndns.org',i:5,now:now.getTime()}
			];
			$('#webcam-preload').html('');
			var de = $('#webcamTemplate2').tmpl(cams);
			$('#webcamTemplate2').tmpl(cams).appendTo("#webcam-preload");
			this.pic1 = new Image(10,10);
			this.pic1.src=$('#webcam-preload img:eq(0)').attr('src');
			this.pic2 = new Image(10,10);
			this.pic2.src=$('#webcam-preload img:eq(1)').attr('src');
			this.pic3 = new Image(10,10);
			this.pic3.src=$('#webcam-preload img:eq(2)').attr('src');
			this.pic4 = new Image(10,10);
			this.pic4.src=$('#webcam-preload img:eq(3)').attr('src');
			this.pic5 = new Image(10,10);
			this.pic5.src=$('#webcam-preload img:eq(4)').attr('src');
			this.pic6 = new Image(10,10);
			this.pic6.src=$('#webcam-preload img:eq(5)').attr('src');
			this.buffor.push($('#webcam-preload').html());
		} else {
			if(this.data.Branch.webcam_count==4) {
				var cams = [
					{webcam:this.data.Branch.webcam,i:0,now:now.getTime()}
					,{webcam:this.data.Branch.webcam,i:1,now:now.getTime()}
					,{webcam:this.data.Branch.webcam,i:2,now:now.getTime()}
					,{webcam:this.data.Branch.webcam,i:3,now:now.getTime()}
				];
			} else {
				var cams = [
					{webcam:this.data.Branch.webcam,i:0,now:now.getTime()}
					,{webcam:this.data.Branch.webcam,i:1,now:now.getTime()}
					,{webcam:this.data.Branch.webcam,i:2,now:now.getTime()}
				];
			}
			$('#webcam-preload').html('');
			var de = $('#webcamTemplate').tmpl(cams);
			$('#webcamTemplate').tmpl(cams).appendTo("#webcam-preload");
			this.pic1 = new Image(10,10);
			this.pic1.src=$('#webcam-preload img:eq(0)').attr('src');
			this.pic2 = new Image(10,10);
			this.pic2.src=$('#webcam-preload img:eq(1)').attr('src');
			this.pic3 = new Image(10,10);
			this.pic3.src=$('#webcam-preload img:eq(2)').attr('src');
			this.pic4 = new Image(10,10);
			this.pic4.src=$('#webcam-preload img:eq(3)').attr('src');
			this.buffor.push($('#webcam-preload').html());
		}
	}
	this.download_loop();
}

webcams.prototype.download_loop=function(){
	var that = this;
	if(this.run) {
		if(this.firstrun) {
			this.show();
			this.firstrun=false;
		}
		if(this.buffor.length>=this.buffor_size)
			this.timer = setTimeout(function(){
				that.download();
			},this.refresh_time);
		else
			this.timer = setTimeout(function(){
				that.download();
			},500);
	}
}


webcams.prototype.show=function(){
	if(this.firstrun || this.buffor.length>=this.buffor_size) {
		$('#webcam-r .webcam-old').remove();
		$('#webcam-r img').addClass('webcam-old');
		$('#webcam-r').append(this.buffor.shift());
		this.firstrun=false;
	}
	var that=this;
	if(this.run) {
		this.timer2 = setTimeout(function(){
			that.show();
		},this.refresh_time);
	}
}

webcams.prototype.stop= function() {
	this.run=false;
	clearTimeout(this.timer);
	clearTimeout(this.timer2);
}

function rotator(settings){
	this.settings = $.extend({
		container:$('#rotator_content')
		,controls:null
		,selected_content_class:'rotator_content_selected'
		,selected_control_class:null
		,bg:null
		,delay:500//fadeIn/Out delay in ms
		,slideshow:true//slideshow?
		,slideshow_delay:5000//delay between elements
		,prev:null
		,next:null
	},settings);
	this.init();
	this.current_id=0;
};

rotator.prototype.init=function(){
	this.settings.container.children().hide();
	this.uniqid = (new Date).getTime()*2;
	var that = this;
	$.each(this.settings.container.children(),function(i,item) {
		$(item).attr('id','rotator-content-'+that.uniqid+'-'+i);
	});
	if(this.settings.controls) {
		$.each(this.settings.controls,function(i,item) {
			$(item).attr('id','rotator-control-'+that.uniqid+'-'+i);
		});
	}
	if(this.settings.bg) {
		$.each(this.settings.bg,function(i,item) {
			$(item).attr('id','rotator-bg-'+that.uniqid+'-'+i);
		});
		this.settings.bg.hide();
	}
	if(this.settings.controls) {
		this.settings.controls.click(function(){
			that.settings.slideshow=false;//turn off slideshow
			var id = this.id;
			id=id.replace('rotator-control-'+that.uniqid+'-', '');
			that.select(parseInt(id));
		});
	}
	if(this.settings.prev) {
		this.settings.prev.click(function(){
			that.settings.slideshow=false;
			that.current_id--;
			if(that.current_id<0)
				that.current_id=that.count-1;
			that.select(that.current_id);
		});
	}
	if(this.settings.next) {
		this.settings.next.click(function(){
			that.settings.slideshow=false;
			that.current_id++;
			if(that.current_id>=that.count)
				that.current_id=0;
			that.select(that.current_id);
		});
	}
	this.select(0);
	this.slideshow_current=0;
	this.count = this.settings.container.children().length;
	setTimeout(function(){that.slideshow()},this.settings.slideshow_delay);
}
rotator.prototype.select=function(id) {
	this.current_id=id;
	var that = this;
	$('.'+this.settings.selected_control_class).removeClass(this.settings.selected_control_class);
	$('#rotator-control-'+this.uniqid+'-'+id).addClass(this.settings.selected_control_class);
	$('.'+this.settings.selected_content_class).fadeOut(this.settings.delay).removeClass(this.settings.selected_content_class);
	if(that.settings.bg)
		that.settings.bg.fadeOut(this.settings.delay);
	setTimeout(function(){
	$('#rotator-content-'+that.uniqid+'-'+id).addClass(that.settings.selected_content_class).fadeIn(that.settings.delay);
	if(that.settings.bg)
		$('#rotator-bg-'+that.uniqid+'-'+id).fadeIn(that.settings.delay);
	},this.settings.delay);
}
rotator.prototype.slideshow=function(){
	var that=this;
	if(this.settings.slideshow) {
		this.slideshow_current++;
		if(this.slideshow_current>=this.count)
			this.slideshow_current=0;
		this.select(this.slideshow_current);
		setTimeout(function(){that.slideshow()},this.settings.slideshow_delay);
	}
}
