
//checks if element/s exists and saves it in global variable $ee - element exists
function $e(selector){return (elements = $(selector)).length>0 ? window['$ee'] = elements : false}

//clear value
function ci(elem){
		elem.value = '';
}

function fbs_click(url, title)
{
	u = (url ? url : location.href);
	t = (title ? title : document.title);
	window.open('http://www.facebook.com/sharer.php?u=' + encodeURIComponent(u) + '&t=' + encodeURIComponent(t), 'sharer', 'toolbar=0,status=0,width=626,height=436');
	return false;
}

$(document).ready(function() {

	 // add js to body for css
	 $(document.body).addClass('js')
	 if(jQuery.browser.safari) $(document.body).addClass('safari')


	/*Jump menu function for dropdowns*/
	$('.jump_menu').bind('change', function(){
		window.location.href = $(this).val();
	});

	 // Vertical menu brands
	 var acc_last = {};



	// if IE 6
 	if (jQuery.browser.msie && jQuery.browser.version == "6.0") {
		$('span.btn').hover(function(){$(this).addClass('btn_hover')},function(){$(this).removeClass('btn_hover')})
		$('.tt_3 td').hover(function(){$(this).closest('.tt_nav').addClass('tt_3_hover')},function(){$(this).closest('.tt_nav').removeClass('tt_3_hover')})
		$('.tt_2 td').hover(function(){$(this).closest('.tt_nav').addClass('tt_2_hover')},function(){$(this).closest('.tt_nav').removeClass('tt_2_hover')})
		$('.package .options').hover(function(){$('select').css('visibility','hidden')},function(){$('select').css('visibility','visible')})

		//IE6 alpha
		if($e(".special-box h2, .attribute-desc .attribute-icon")) $ee.pngFix({method:'crop'});
	}


	// Left menu collapsing
	 $('.special-collapse .collapse ul').hide()
	 acc_last['ul'] = $('.special-collapse .collapse-open ul')
	 acc_last['h2'] = acc_last['ul'].parent().addClass('collapse-open').find('h2')
	 acc_last['ul'].show("blind",null,"fast");

	 function specialCollapse(){
		if(acc_last['h2'] != this){
			 if (acc_last['h2']) $(acc_last['h2']).parent().removeClass('collapse-open')
			 if (acc_last['ul']) acc_last['ul'].hide("blind",null,"fast");

			 acc_last['h2'] = this

			 $(this).parent().addClass('collapse-open')
			 acc_last['ul'] = $(this).parent().find('ul').show("blind",null,"fast");
		 }
	 }

	var hoverIntentConfig = {
		sensitivity: 3,
		interval: 200,
		over: specialCollapse,
		timeout: 500,
		out: function(){}
	};

	$('.special-collapse.special-collapse-has-items .collapse h2').hoverIntent(hoverIntentConfig);

	//Table classes
	$('tr:odd').addClass('odd')
	$('tr:first-child').addClass('first')
	$('tr:last-child').addClass('last')
	$('tr:even').addClass('even')
	$('tr > td:first-child, tr > th:first-child').addClass('first')
	$('tr > td:last-child,	tr > th:last-child').addClass('last')
	$('tr').each(function(index, row){
		n=0;
		$(row).find('td, th').each(function(i, cell){
			if(!$(cell).attr("class")) $(cell).addClass('col'+(++n))
		})
	})

	//package
	$('.packages .package .body p:last-child, .packages .package .bubble p:last-child').addClass('lastone')
	$('.packages .package:nth-child(3n-2)').addClass('first-package-in-row')



	//specialboxes equal height
 // if($e('.height_group .special-box .in2')) $ee.equalizeCols()

//	if($e('.packages .package .body')) $ee.equalizeCols()

	if($e('.height_group')) $ee.each(function(i,g){
		$(g).find('.box-in').equalizeCols()
		$(g).find('.package .body').equalizeCols()
		$(g).find('.box-in, .package').equalizeCols()
		$(g).find('.special-box .in2').equalizeCols()
	})

	if($e('.ec')) $ee.equalizeCols(null, true, 14)


    $('.hbo_teaser').cycle({
    	timeout: 6000
    });

	//Za kuću
	if($e('#tabbed_teaser .tt_tabs')){
		$ee.cycle({
				fx:		 'fade',
				speed:	'fast',
				timeout: 0,
				pager:	'#tabbed_teaser .tt_nav',
				pagerEvent: 'click',
				pagerAnchorBuilder: function(idx, slide) {
						return '#tabbed_teaser .tt_nav td.handle_' + idx;
				}
		});
	}



	//Select + gumb => go to url
	$('.trazilica input.btn').click(function(){
	 window.location.href = $(this).parents('td.trazilica').find('select').val();
	})


	//Packages Bubbles
	$('.package .options').hover(
		function(){
			$(this).addClass('options-hover')
		},function(){
		 $(this).removeClass('options-hover')
	 });

	 $('#superbrands').addClass('refresh').removeClass('refresh');

	$('#contact-me-form-submit').click(function(e) {
		e.preventDefault();
		var form = $(this).closest('#contact-me-form');

		var _name_surname = form.find('input[name="NameSurname"]');
		var _phone_number = form.find('input[name="PhoneNumber"]');
		var _mobile_phone_number = form.find('input[name="MobilePhoneNumber"]');
		var _page_title = $(document).find('title').html().replace(/^\s+|\s+$/g, '');

		var name_surname = _name_surname ? _name_surname.val().replace(/^\s+|\s+$/g, '') : '';
		if ( name_surname == 'ime i prezime' ) name_surname = '';

		var phone_number = _phone_number ? _phone_number.val().replace(/^\s+|\s+$/g, '') : '';
		if ( phone_number == 'broj telefona' ) phone_number = '';

		var mobile_phone_number = _mobile_phone_number ? _mobile_phone_number.val().replace(/^\s+|\s+$/g, '') : '';
		if ( mobile_phone_number == 'broj mobitela' ) mobile_phone_number = '';

		var page_title = _page_title.split(' - ');
		if ( page_title.length > 2 ) {
			page_title = page_title[0] + ' / ' + page_title[2];
		}
		else {
			page_title = _page_title;
		}

		if ( name_surname.length == 0 ) {
			displayFancyBoxMessage( 'Molimo Vas upišite Vaše ime i prezime.' );
			return;
		}

		if ( phone_number.length == 0 && mobile_phone_number.length == 0 ) {
			displayFancyBoxMessage( 'Molimo Vas upišite barem jedan telefonski broj.' );
			return;
		}

		form.find('.submit-button-bg').hide();
		form.find('.submit-button-loader').show();

		$.ez( 'ezjscIskon::submitContactMeForm', {name_surname: name_surname, phone_number: phone_number, mobile_phone_number: mobile_phone_number, page_title: page_title}, function( data ) {
			if (data.content.status == 'success') {
				form.closest('.contact-me-form-right').find('.contact-me-form-right-inner').fadeOut(600, function() {
					parent_div = $(this).parent();
					$(this).remove();
					parent_div.html(data.content.content);
					parent_div.find('.contact-me-form-right-inner').fadeIn(600);
				});
			}
			else {
				form.find('.submit-button-loader').hide();
				form.find('.submit-button-bg').show();

				displayFancyBoxMessage( data.content.content );
			}
		});
	});

	function displayFancyBoxMessage( message ) {
		$.fancybox({
			content: '<p>' + message + '</p>',
			showNavArrows: false,
			centerOnScroll: false,
			overlayShow: false,
			autoDimensions: false,
			autoScale: false,
			width: 228,
			height: 175,
			scrolling: 'no',
			customClass: 'fancybox-message'
		});
	}

	/*var element = $('#box-order-package');
	var position = element.offset();
	var top-position = position.top;
	var left-position = position.left;*/
	
	/*$("#fancybox-wrap").offset({ top: 10, left: 30 });*/

});

