/**
 * @author GeorgeGG
 */
//Additional JavaScript Functions (for IE!!!):
if(!Array.indexOf){
    Array.prototype.indexOf = function(obj){
        for(var i=0; i<this.length; i++){
            if(this[i]==obj){
                return i;
            }
        }
        return -1;
    }
}


//Additional JQuery Functions:
jQuery.fn.exists = function(){return jQuery(this).length>0;}

function resetCluetip() {
    $('td').each(function() {
            $(this).cluetip('destroy');
    });
} 

function initClues(){
	resetCluetip()
	if ($('td.event').exists()) {
		$('td.event').cluetip({
			cluetipClass: 'jtip',
			arrows: true,
			dropShadow: false,
			hoverIntent: true,
			sticky: true,
			mouseOutClose: true,
			closePosition: 'title',
			ajaxCache: false,
			closeText: '<img src="resources/scripts/jquery-cluetip/images/close.gif" alt="close" />',
			fx: {
				open: 'fadeIn'
			},
//			hoverIntent: {    
//				sensitivity:  4,
//				interval:     450,
//				timeout:      0
//    		},
			ajaxProcess: function(data) {
               if(data.success){
			   		var _data = data.data;
			   		var html = '<ul>';
					var imax = _data.length;
//					var imax = Math.min(5, _data.length);
			   		for(i=0; i< imax; i++){
						html += '<li><a href="index.php?lang='+_data[i].lang_code+'&sec='+_data[i].section_id+'&ctg='+_data[i].category_id+'&cid='+_data[i].content_id+'">'+ (i+1) +'. '+ _data[i].title +'</a></li>';
					}
					html+='</ul>';
//					if(imax < _data.length){
						_data[imax-1].lang_code == 'gr' ? html += '<a href="index.php?lang='+_data[imax-1].lang_code+'&sec='+_data[imax-1].section_id+'">Δείτε όλες τις εκδηλώσεις</a>' : 
							html += '<a href="index.php?lang='+_data[imax-1].lang_code+'&sec='+_data[imax-1].section_id+'">View all events</a>'
//					}
					return html;
			   } else {
			   		return 'Error';
			   }
			},
			ajaxSettings: {   
				dataType: 'json'
    		}
		});
	}
}

$(document).ready(function() {
	if (document.location.href.indexOf('cid') == -1) {
		$("#print").hide();
	}
	
	$('div#right_menu div:first-child').attr('class', 'right_menu_first');
	
	if ($("#content_body").exists()){
			$("#content_body").addClass('normal');
	}
	var _lang = 'gr';
    var _image = 'Εικόνα';
    var _of = 'από';
	if (document.location.href.indexOf('lang') != -1){
		if(document.location.href.indexOf('lang=gr') == -1){
            if(document.location.href.indexOf('lang=en') == -1){
                _lang = 'bg';
                _image = 'Изображение';
                _of = 'от';
            } else {
                _lang = 'en';
                _image = 'Image';
                _of = 'of';
            }
		}
	}
	$('#languages li a[id='+_lang+']').addClass('selected');
	$('#languages li a[id!='+_lang+']').removeClass('selected');
	
	var cdate = new Date();
	
	if ($('#cal').exists()) {
		$('#cal').calendar({
			triggerElement: null,
			parentElement: '#calendar_wrapper',
			dateFormat: '%d-%m-%Y',
			lang: _lang,
			selectHandler: function(calendar){
				
			}
		});
	}
	
	if(jQuery.browser.msie && jQuery.browser.version < 7){
		$(document).pngFix(); 
	}
	
	if ($('td.event').exists()) {
		$('td.event').cluetip({
//			splitTitle: '|',
			cluetipClass: 'jtip',
			arrows: true,
			dropShadow: false,
			hoverIntent: true,
			sticky: true,
			mouseOutClose: true,
			closePosition: 'title',
			closeText: '<img src="resources/scripts/jquery-cluetip/images/close.gif" alt="close" />',
			fx: {
				open: 'fadeIn'
			},
//			hoverIntent: {    
//				sensitivity:  4,
//				interval:     450,
//				timeout:      0
//    		},
			ajaxProcess: function(data) {
               if(data.success){
			   		var _data = data.data;
			   		var html = '<ul>';
					var imax = _data.length;
//					var imax = Math.min(5, _data.length);
			   		for(i=0; i< imax; i++){
						html += '<li><a href="index.php?lang='+_data[i].lang_code+'&sec='+_data[i].section_id+'&ctg='+_data[i].category_id+'&cid='+_data[i].content_id+'">'+ (i+1) +'. '+ _data[i].title +'</a></li>';
					}
					html+='</ul>';
//					if(imax < _data.length){
						_data[imax-1].lang_code == 'gr' ? html += '<a href="index.php?lang='+_data[imax-1].lang_code+'&sec='+_data[imax-1].section_id+'">Δείτε όλες τις εκδηλώσεις</a>' : 
							html += '<a href="index.php?lang='+_data[imax-1].lang_code+'&sec='+_data[imax-1].section_id+'">View all events</a>'
//					}
					return html;
			   } else {
			   		return 'Error';
			   }
			},
			ajaxSettings: {   
				dataType: 'json'
    		}
		});
	}
	if ($('#slider').exists()) {
		if (!jQuery.browser.msie) {
			$('#slider ul').cycle({
				fx: 'fade',
				pause: 1,
				random: 1,
				speed: 3000,
				timeout: 10000,
				cleartype: 1,
				nowrap: 1,
				easing:'swing',
				before: function(currSlideElement, nextSlideElement, options, forwardFlag){
					if ($(currSlideElement).children('div').is(':visible')) {
						$(currSlideElement).children('div').slideUp(800)
					};
					$(nextSlideElement).children('div').hide();
				},
				after: function(currSlideElement, nextSlideElement, options, forwardFlag){
					$(nextSlideElement).children('div').slideDown(800);
				}
			});
		} else {
			$('#slider ul').cycle({
				fx: 'fade',
				pause: 1,
				random: 1,
				speed: 3000,
				timeout: 10000,
				easing:'swing',
				cleartype: 1,
				nowrap: 1,
				before: function(currSlideElement, nextSlideElement, options, forwardFlag){
					if ($(currSlideElement).children('div').is(':visible')) {
						$(currSlideElement).children('div').hide()
					};
					$(nextSlideElement).children('div').hide();
				},
				after: function(currSlideElement, nextSlideElement, options, forwardFlag){
					$(nextSlideElement).children('div').slideDown(800);
				}
			});
		}

		$('#slider').fadeIn();
	}
	$('#weather_cities').cycle({
	    fx: 'fade',
		pause: 1,
		random: 1,
	    speed: 3000,
	    timeout: 8000,
		cleartype:1,
		easing: "swing",
		nowrap:1,
		sync:0
	 });
	 $('.drawers-wrapper').show();

	$(function() {
		var menu_item = null;
		
		$('#menu li.mouseover').each(function() {
			menu_item = $(this).attr("id");
		});
		$('#menu li').mouseout(function() {
			if ($(this).attr("id") != menu_item) {
				$(this).toggleClass("mouseover");
			}
			return false;
		});
		$('#menu li').mouseover(function() {
			if ($(this).attr("id") != menu_item) {
				$(this).toggleClass("mouseover");
			}
			return false;
		});
		$('#menu li').click(function() {
			if($.cookie('accordion-cookie')){
				$.cookie('accordion-cookie', null);
			}
			if($.cookie('accordion2-cookie')){
				$.cookie('accordion2-cookie', null);
			}
		});
	});
	
	$('#breadcrumbs li').click(function(){
		if($.cookie('accordion-cookie')){
			$.cookie('accordion-cookie', null);
		}
		if($.cookie('accordion2-cookie')){
			$.cookie('accordion2-cookie', null);
		}
	});

	//enable the lightbox for all the links with class lightbox and lightbox-group
	$('a.lightbox').lightBox({
		overlayOpacity: 0.8,
		imageLoading: 'http://www.visitevros.gr/resources/scripts/lightbox/images/lightbox-ico-loading.gif',
		imageBtnClose:'http://www.visitevros.gr/resources/scripts/lightbox/images/lightbox-btn-close.gif',
		imageBtnPrev: 'http://www.visitevros.gr/resources/scripts/lightbox/images/lightbox-btn-prev.gif',
		imageBtnNext: 'http://www.visitevros.gr/resources/scripts/lightbox/images/lightbox-btn-next.gif',
		containerResizeSpeed: 350,
		txtImage: _image,
		txtOf: _of
	});

	$('a#print').click(function() {
		window.open(this.rel, 'printable', 'width=850, height=600, scrollbars=yes, resizable=yes');
		return false;
	});
	$('a#resize').click(function() {
		var el_class = $("#content_body").attr("class");
		if (el_class == '') {
			el_class = 'normal';
		}
		
		switch(el_class) {
			case 'large':
				$('#content_body').removeClass(el_class);
				$('#content_body').addClass('x-large');
				el_class = 'x-large';
				break;
			case 'x-large':
				$('#content_body').removeClass(el_class);
				$('#content_body').addClass('normal');
				el_class = 'normal';
				break;
			case 'normal': //intended
			default: 
				$('#content_body').removeClass(el_class);
				$('#content_body').addClass('large');
				el_class = 'large';
				break;
		}
		return false;
	});

	jQuery.each(jQuery.browser, function(i) {
		if($.browser.msie) {
			$("a#contact").click(function(event) {
				event.preventDefault();
				window.open(this.href, 'printable', 'width=600, height=500, scrollbars=yes, resizable=yes');
				return false;
			});
			$("a#email").click(function(event) {
				event.preventDefault();
				window.open(this.href, 'printable', 'width=600, height=450, scrollbars=yes, resizable=yes');
				return false;
			});
			$("a#registerCompany").click(function(event) {
				event.preventDefault();
				window.open(this.href, 'printable', 'width=600, height=600, scrollbars=yes, resizable=yes');
				return false;
			});
		} else {
			$("a#contact").ceebox({
				borderColor: '#CE6700',
				htmlHeight: 500,
				htmlWidth: 600,
				titles: false
			});
			$("a#email").ceebox({
				borderColor: '#CE6700',
				htmlHeight: 450,
				htmlWidth: 600,
				titles: false
			});
			$("a#registerCompany").ceebox({
				borderColor: '#CE6700',
				htmlHeight: 600,
				htmlWidth: 600,
				titles: false
			});
		}
	});
});
