$(function(){	
	
	removeNotepadButtonControl("#notatnik-ofert .remove-button");
	removeOstatnioPrzegladaneButtonControl("#ostatnio-przegladane .remove-button");
	notatnikIconControl();
	wyczyscNotatnik();
	listTypeControl();
	generateHints();
	listSort();
	hideThirdProperty();
});


function createGallery(off_id, gallery_id)
{
	//alert(off_id);
	$.ajax({
		url: "handler/offerGallery/"+off_id,		  
		success: function(data){
			
			$('#'+gallery_id).hide();
			$('#'+gallery_id).html(data);
			
			setTimeout(function(){								  
			  	Galleria.loadTheme('js/themes/classic/galleria.classic.js');		    
			    		   
				$('#'+gallery_id).galleria({
				width:350,
				height:205,
				transition: 'fade',
				lightbox: true
				});
				
				$('#'+gallery_id).show();
				
			}, 500);
		}
	});
}

function createInvestGallery(inv_id, gallery_id)
{
	//alert(off_id);
	$.ajax({
		url: "handler/investmentGallery/"+inv_id,		  
		success: function(data){
			
			$('#'+gallery_id).html(data);				  
		  	Galleria.loadTheme('js/themes/classic/galleria.classic.js');		    
		    		   
			$('#'+gallery_id).galleria({
			width:350,
			height:205,
			transition: 'fade',
			lightbox: true
			});
		}
	});
}

function pokazNaMapie(lat, long, div_id)
{	    
    var latlng = new google.maps.LatLng(lat, long);
	var mapOptions = {
	    zoom: 8,
	    center: latlng,
	    mapTypeId: google.maps.MapTypeId.ROADMAP
	  };
	
	$(function(){
		var map = new google.maps.Map(document.getElementById(div_id), mapOptions);				
        var marker = new google.maps.Marker({position: latlng,map: map});        
	});
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function DodajDoNotatnika(Id, msg, el) {
    var c = readCookie("notatnik");
    
    if (c === null) c = "";
    
    if (c.indexOf(Id+",") == -1) c += Id+",";
    
    eraseCookie("notatnik");

    createCookie("notatnik", c, 31);
    
    $(el).parent().attr('class', 'usun-z-notatnika');
    $(el).parent().find(".notatnik-usun").show();
    $(el).hide();
    //if (msg != "") setTimeout(function () { alert(msg); }, 100);
    
    //if($('#notatnik-ofert .side-ofe-box').length == 0) $('#notatnik-ofert .notepad-info').slideUp(500) ;
    
    if($('#notatnik-ofert .side-ofe-box').length < 5)
    {
	    $.ajax({
			url: "handler/genNotepadPosition/"+Id,		  
			success: function(data){
			$('#notatnik-ofert').append(data);
			$('.fresh-in-notepad').slideDown(600, function(){
				//$(this).removeClass('fresh-in-notepad');
			});
			removeNotepadButtonControl('#notatnik-ofert .remove-button[rel=oferta-'+Id+']');
			}
	    });
    }
    
    var offersCount = parseInt($('#notatnik-ofert h2 span').text())+1;
    
    $('#notatnik-ofert h2 span').text(offersCount);
    
    if(offersCount > 0)
    {
    	$('#notatnik-ofert .notepad-info').slideUp(500);
    	$('#notatnik-wszystkie').slideDown(500);
    }
    //else $('#notatnik-ofert .notepad-info').slideDown(500);
    
    $.jGrowl(infoText['dodano-do-notatnika'], { life: 600 , position: 'bottom-right' });


    //showInfo(infoText['dodano-do-notatnika']);
}

function UsunZNotatnika(Id, msg) {
    var c = readCookie("notatnik");
    
    if (c === null) c = "";
    
    if (c.indexOf(Id+",") > -1) {
        c = c.replace(Id+",", "");
    }
    
    eraseCookie("notatnik");

    createCookie("notatnik", c, 31);
    
    var icon_selector = '.notatnik-usun[rel=oferta-'+Id+']';
    var icon = $(icon_selector);    
    $(icon).parent().attr('class', 'dodaj-do-notatnika');
    $(icon).parent().find(".notatnik-dodaj").show();
    $(icon).hide();    
    
    var list_element_selector = '#notatnik-ofert .remove-button[rel=oferta-'+Id+']';
    $(list_element_selector).parent().slideUp(400, function(){
    	$(this).remove();    	
    	if (msg != "") setTimeout(function () { alert(msg); }, 100);
    });
    $(list_element_selector).hide();
    
    $('#notatnik-'+Id).slideUp(500) ;
    
    var offersCount = parseInt($('#notatnik-ofert h2 span').text())-1;
    
    $('#notatnik-ofert h2 span').text(offersCount);
    if(offersCount == 0)
    {
    	$('#notatnik-ofert .notepad-info').slideDown(500);
    	$('#notatnik-wszystkie').slideUp(500);
    }
    
    $.jGrowl(infoText['usunieto-z-notatnika'], { life: 600 , position: 'bottom-right' });    
}

function removeNotepadButtonControl(selector)
{	
	$(selector).click(function(){
		var tab = $(this).attr('rel').split('-');
		var id = tab[1];		
		UsunZNotatnika(id, '', $('.usun-z-notatnika .notatnik-usun-'+id));
		return false;
	});		
}

function notatnikIconControl()
{	
	$('.notatnik-dodaj').click(function(){		
		var tab = $(this).attr('rel').split('-');
		var id = tab[1];		
		DodajDoNotatnika(id, '', this);		
		return false;
	});
	
	$('.notatnik-usun').click(function(){		
		var tab = $(this).attr('rel').split('-');
		var id = tab[1];
		
		$('#notatnik-'+id).slideUp(500);
		
		UsunZNotatnika(id, '', $('.usun-z-notatnika .notatnik-usun-'+id));
		return false;
	});
}

function wyczyscNotatnik()
{
	$('.wyczysc-notatnik').click(function(){
		
		eraseCookie("notatnik");
		//$('.ofe-box-list').fadeOut(500);		
		recuRemoveFromSideNotepad();
		recuRemoveFromNotepad();
		$('#notatnik-ofert h2 span').text('0');
		$('#notatnik-wszystkie').slideUp(500);
	});
}

function recuRemoveFromNotepad()
{
	if($('.ofe-box-list').length > 0)
	{
		$('.ofe-box-list').first().slideUp(300, function(){
			$(this).remove();
			recuRemoveFromNotepad();
		});
	}
}

function recuRemoveFromSideNotepad()
{
	if($('#notatnik-ofert .side-ofe-box').length > 0)
	{
		$('#notatnik-ofert .side-ofe-box').first().slideUp(300, function(){
			$(this).remove();
			recuRemoveFromSideNotepad();
		});
	}
	else $('#notatnik-ofert .notepad-info').slideDown(500);
}

var allow_animate = true;
function showInfo(info)
{	
	if(allow_animate)
	{
		allow_animate = false;
		var infoBox = '<p id="show-info" style="display: none;">'+info+'</p>';
		$('body').append(infoBox);
		$('#show-info').fadeIn(200, function(){
			$(this).fadeOut(700, function(){
				$(this).remove();
				allow_animate = true;
			});
		});
	}
}


function listTypeControl()
{
	$('.main-lista-ofert').click(function(){
		setListType('lista');
		$('.ofe-box').fadeOut(150, function(){
			changeListHTML('lista');
			$('.ofe-box-list').fadeIn(150);
		});
		return false;
	});
	
	$('.main-boxy-ofert').click(function(){
		setListType('boxy');
		$('.ofe-box-list').fadeOut(150, function(){
			changeListHTML('boxy');
			$('.ofe-box').fadeIn(150);
		});
		return false;
	});
}

function changeListHTML(type)
{
	if(type == 'boxy')
	{
		$('.ofe-skrot, .ofe-icon-menu').hide();		
		$('.ofe-opis').each(function(){
			if($(this).find('p').length == 3) $(this).find('p').last().hide();
		});
		$('.ofe-box-list').removeClass('ofe-box-list').addClass('ofe-box');
	}
	
	if(type == 'lista')
	{
		$('.ofe-skrot, .ofe-icon-menu').show();
		$('.ofe-opis').each(function(){
			if($(this).find('p').length == 3) $(this).find('p').last().show();
		});
		$('.ofe-box').addClass('ofe-box-list').removeClass('ofe-box');
	}	
}

function hideThirdProperty()
{
	$('.ofe-box .ofe-opis').each(function(){
		if($(this).find('p').length == 3) $(this).find('p').last().hide();
	});
}

function setListType(type)
{
	eraseCookie("listtype");
    createCookie("listtype", type, 31);   
}

function generateHints()
{
	$('.ofe-icon-menu a, .show-hint').each(function(){
		var text = $(this).text();
		$(this).text('');
		$(this).tipsy(
			{
				gravity: 'n',
				fade: true,
				fallback: text
			}
		);
	});
}


function removeOstatnioPrzegladaneButtonControl(selector)
{	
	$(selector).click(function(){
		var tab = $(this).attr('rel').split('-');
		var id = tab[1];		
		UsunOstatnioPrzegladane(id, '');
		return false;
	});		
}


function UsunOstatnioPrzegladane(Id, msg) {
	
	var c = readCookie("przegladane");
	c = unescape(c);	
	//alert(c);
	
    if (c === null) c = "";    
    
    if (c.indexOf(Id+",") > -1) {
        c = c.replace(Id+",", "");
        //alert(c);
    }
    
    eraseCookie("przegladane");

    createCookie("przegladane", c, 31);
    
    var list_element_selector = '#ostatnio-przegladane .remove-button[rel=oferta-'+Id+']';
    
    $(list_element_selector).parent().slideUp(400, function(){
    	$(this).remove();    	
    	if (msg != "") setTimeout(function () { alert(msg); }, 100);
    });    
        
    if($('#ostatnio-przegladane .side-ofe-box').length == 1) $('#ostatnio-przegladane').slideUp(500);
    
    $.jGrowl(infoText['usunieto-ostatnio-przegladane'], { life: 600 , position: 'bottom-right' });    
}

function listSort()
{
	$('#sort-type a').click(function(){
		var rel = $(this).attr('rel');
		var sort = rel.split('-');
		var sort_by = sort[0];
		var sort_dest = sort[1];
				
		$('#sort-form input[name=sort_by]').val(sort_by);
		$('#sort-form input[name=sort_dest]').val(sort_dest);
		$('#sort-form').submit();
		//alert($(this).attr('rel'));
	});
}

