//hs.graphicsDir = '/highslide/graphics/';

jQuery(function() {


    // Reset Font Size
    var originalFontSize = jQuery('.news').css('font-size');
        jQuery(".size").click(function(){
        jQuery('.news').css('font-size', originalFontSize);
    });
    // Increase Font Size
    jQuery(".increaseFont").click(function(){
        var currentFontSize = jQuery('.news').css('font-size');
        var currentFontSizeNum = parseFloat(currentFontSize, 10);
        var newFontSize = currentFontSizeNum*1.2;
        jQuery('.news').css('font-size', newFontSize);
        return false;
    });
    // Decrease Font Size
    jQuery(".decreaseFont").click(function(){
        var currentFontSize = jQuery('.news').css('font-size');
        var currentFontSizeNum = parseFloat(currentFontSize, 10);
        var newFontSize = currentFontSizeNum*0.8;
        jQuery('.news').css('font-size', newFontSize);
        return false;
    });



    jQuery('.bannertracker').click(function(e) {
        link = '/bannertracker.php?tracker='+this.id;
        dataString = '';
		jQuery.ajax({
            type: "POST",
            data: dataString,
            url: link,
            success: function(data) {
                //alert(data);
            }
        });
    });


    jQuery.datepicker.setDefaults(jQuery.extend({showMonthAfterYear: false}, jQuery.datepicker.regional['']));
    jQuery("#calendar").datepicker(jQuery.datepicker.regional['lv']);

});



function submitpoll(id_poll,container){
    var pid = id_poll;
    var checkValues = [];
    jQuery('#'+container+' input:checked').each(function() {
        checkValues.push(jQuery(this).val());
    });
    var checkSegmentStatus = checkValues.join(",");
    if(checkSegmentStatus==''){
        alert('Atzīmējiet atbildi!');
        return;
    }

    jQuery.post("/"+language+"/poll/submitpoll/", {pid:pid, answers: checkSegmentStatus },
	    function(data) {
            jQuery('#'+container+'').hide();
            jQuery('#'+container+'').html(data).fadeIn('slow');
            setTimeout('showpollanswers("'+pid+'","'+container+'")',2000);

		}
	);
}

function showpollanswers(pid,container){
    jQuery.post("/"+language+"/poll/showpollanswersinner/", {pid:pid },
	    function(data) {
            jQuery('#'+container+'').hide()
            jQuery('#'+container+'').html(data).fadeIn('slow');
		}
	);


}
function genpasts(prefix,postfix)
{
	if (prefix!='' && postfix!='')
	{
		var pasts=prefix;
			pasts=pasts+'@';
			pasts=pasts+postfix;

		document.write('<a href="mailto:'+pasts+'"  <font class="font1">'+pasts+'</font></a>');
	}
	else document.write('-');
}

function submitQuestionForm(parameter){

    jQuery('#action').val(parameter);
    document.pollForm.submit();

}

function page(l){
    //$('templates_loading').show();
    jQuery('#limit').val(l);
    document.adminForms.submit();

}
function checkforsubcat(){
    var cat = jQuery('#category').val();
    jQuery.post("/usertools/checkfornewscategory/", {F_CATEGORY:cat },
	    function(data) {
            jQuery('#sub_cat').html(data);;
		}
	);
}
function showeventsubcat(id_category){
    jQuery('#eventcat').val(id_category);
    //alert(jQuery('#eventcat').val());
    jQuery('#subcat').html('');
    jQuery('#subcat').addClass('loadingeventsubcat');
    jQuery('#cat a').css('font-weight','normal');
    jQuery.post("/events/checkforeventssubcategory/", {F_CATEGORY:id_category},
	    function(data) {
            jQuery('#cat'+id_category).css('font-weight','bold');
            jQuery('#subcat').html(data);;
            jQuery('#subcat').removeClass('loadingeventsubcat');
		}
	);
}
function showevent(id_event){
    jQuery('#'+id_event).toggle("slow");
}
function cleareventfilter(){
    jQuery('#date_from').val('');
    jQuery('#date_to').val('');
    jQuery('#string').val('');
    jQuery('#subcat').html('');
    jQuery('#eventcat').val('');

    jQuery('#cat a').css('font-weight','normal');

}
function submituserform(action,lang){

    jQuery('#small-login-warning').html('');

    var height = jQuery('#small-login').height();

    jQuery('#small-login-content').hide();
    jQuery('#small-login-loading').height(height);
    jQuery('#small-login-loading').addClass('qloading');


        var inputs = jQuery("#loginform input");
        var queryString = inputs.serialize();


        var link = '/'+language+'/login/large/';
		jQuery.ajax({
            type: "POST",
            url: link,
            data: queryString,
            success: function(data) {
                data = data.split('#');
                if(data[0]==0){

                    jQuery('#small-login-warning').html(data[1]);
                    jQuery('#F_PASSWORD').val('');
                    jQuery('#small-login-loading').removeClass('qloading');
                    jQuery('#small-login-loading').height('');
                    jQuery('#small-login-content').show();

                }else{
                    //window.location.reload();
                    window.location.href=data[1];
                }
            }
     });

}
function searchKeyPress(e,button){
    // look for window.event in case event isn't passed in
    if (window.event) { e = window.event; }
    if (e.keyCode == 13){
        document.getElementById(button).click();
    }
}
function submitKeyPress(e,button){
    // look for window.event in case event isn't passed in
    if (window.event) { e = window.event; }
    if (e.keyCode == 13){
        document.getElementById(button).click();
    }
}
function submitcomment(){
    //var name = jQuery('#name').val();

    //var comment = jQuery('#comment').val();
    //if(name.length<2 || comment.length<10 || jQuery('#code').val()!=jQuery('#code_hidden').val()){
    var dataString = jQuery('#commform').serialize();
    jQuery('#commentform').html('');
    jQuery('#commentform').addClass('loadingcommentform');
    //jQuery('#commentFormWin').addClass("loadingForm");
        //if(name.length<2){jQuery('#name_warning').show();}else{jQuery('#name_warning').hide();}
        //if(comment.length<10){jQuery('#comment_warning').show();}else{jQuery('#comment_warning').hide();}
        //if(jQuery('#code').val()!=jQuery('#code_hidden').val()){jQuery('#code_warning').show();}else{jQuery('#code_warning').hide();}
    jQuery.ajax({
        type: "POST",
        url: "/news/post_article_comment/",
        data: dataString,
        success: function(data) {
            //alert(data);
            if(data=='1'){
                window.location.href='?opcija=komentari';
            }else{
                jQuery('#commentform').html(data);
                jQuery('#commentform').removeClass('loadingcommentform');
            }
            //document.getElementById('commForm').submit();
            //jQuery('#commentform').show();
        }
    });
    //jQuery('#commentFormWin').removeClass("loadingForm");
    //}else{

    //}
}
function submitimagecomment(id_image){
    //var name = jQuery('#name').val();

    //var comment = jQuery('#comment').val();
    //if(name.length<2 || comment.length<10 || jQuery('#code').val()!=jQuery('#code_hidden').val()){

    var inputs = jQuery("input, textarea");
    var queryString = inputs.serialize();

    //var queryString = jQuery('#commform').serialize();
    jQuery('#commentform').html('');
    jQuery('#commentform').addClass('loadingcommentform');
    //jQuery('#commentFormWin').addClass("loadingForm");
        //if(name.length<2){jQuery('#name_warning').show();}else{jQuery('#name_warning').hide();}
        //if(comment.length<10){jQuery('#comment_warning').show();}else{jQuery('#comment_warning').hide();}
        //if(jQuery('#code').val()!=jQuery('#code_hidden').val()){jQuery('#code_warning').show();}else{jQuery('#code_warning').hide();}
    jQuery.ajax({
        type: "POST",
        url: "/gallery/post_image_comment/",
        data: queryString,
        success: function(data) {
            //alert(data);
            if(data=='1'){
                //jQuery('#imagecommentslist').hide();
                //jQuery('#imagecommentslist').show();
                //alert(id_image);
                var  comm = getcommnetlist(id_image);
                //alert(comm);

            }else{
                jQuery('#commentform').html(data);
                jQuery('#commentform').removeClass('loadingcommentform');
            }
            //document.getElementById('commForm').submit();
            //jQuery('#commentform').show();
        }
    });
    //jQuery('#commentFormWin').removeClass("loadingForm");
    //}else{

    //}
}
function getcommnetlist(id_image){
    var dataString = 'F_ID_IMAGE='+id_image;
    var link = "/gallery/comment_list/";
    //alert(link);
    jQuery.ajax({
        type: "POST",
        url: link,
        data: dataString,
        success: function(data) {
            //alert('west');
            //alert(data);
            jQuery('#imagecommentslist').html(data);
        }
    });
}
function submitform(parameter){
    jQuery('#ListLoadingGif').show();
    jQuery("#F_ACTION").val(parameter);
    //document.getElementById('adminForm').submit();
    jQuery("#adminform").submit();
    //document.adminForm.submit();
}
function creategallery(){
    jQuery('#creatinggallery').show();
    var inputs = jQuery("input,select");
    var queryString = inputs.serialize();
    var link = '/gallery/save_gallery/';
	jQuery.ajax({
        type: "POST",
        url: link,
        data: queryString,
        success: function(data){
            //alert('Galerija dzēsta!');
            if(data=='true'){
                window.location.reload();
            }else{
                jQuery("#warningfield").html(data);
                jQuery('#creatinggallery').hide();
            }
        }
    });
}
function savecomments(){
    var inputs = jQuery("input");
    var queryString = inputs.serialize();
    var link = '/gallery/save_comments/';
	jQuery.ajax({
        type: "POST",
        url: link,
        data: queryString,
        success: function(data){
            alert('Komentāri un secība saglabāta!');
            window.location.reload();
        }
    });
}
function submitgallery(id){
    var queryString = 'F_ID_GALLERY='+id
    var link = '/'+language+'/gallery/confirm_gallery/';
	jQuery.ajax({
        type: "POST",
        url: link,
        data: queryString,
        success: function(data){
            alert('Galerija iesniegta portāla administrācijai apstiprināšanai');
            window.location.reload();
        }
    });
}
function removeimage(id){
    var queryString = 'F_ID_IMAGE='+id
    var link = '/'+language+'/gallery/delete_image/';
	jQuery.ajax({
        type: "POST",
        url: link,
        data: queryString,
        success: function(data){
            jQuery('#image'+id).hide();
        }
    });
}
function deletegallery(id_gallery){

    var dataString = 'F_ID_GALLERY='+id_gallery;
    var link = '/'+language+'/gallery/delete_gallery/';
	jQuery.ajax({
        type: "POST",
        url: link,
        data: dataString,
        success: function(data){
            //alert('Galerija dzēsta!');
            window.location.reload();
        }
    });

}
function add_reply_form(field,id,msg){
        jQuery("#"+field).html("");
        jQuery("#"+field).addClass('replyloading');
        var dataString = 'F_ID_REPLY='+field+'&F_ID_TOPIC='+id+'&F_MSG='+msg;
        var link = '/'+language+'/forum/add_reply_form/';
		jQuery.ajax({
            type: "POST",
            url: '/'+language+"/forum/add_reply_form/",
            data: dataString,
            success: function(data){
                if(data=='false'){
                    window.location.href='/';
                }else if(jQuery("#"+field).html()==""){
                    jQuery("#"+field).html(data);
                }else{
                    jQuery("#"+field).html("");
                }
                jQuery("#"+field).removeClass('replyloading');
            }
     });
}
function add_reply(field,id,forma,hideform){

        jQuery("#"+hideform).height(jQuery('#'+field).height());
        jQuery("#"+hideform).show();

        var link = '/'+language+'/forum/add_reply_form/';
		jQuery.ajax({
            type: "POST",
            url: link,
            data: jQuery('#'+forma).serialize(),
            success: function(data){
                if(data=='true'){
                    window.location.href='?tid='+id;
                }
                if(data=='false'){
                    window.location.href='/';
                }
                if(data!='false' && data!='true'){
                    jQuery("#"+field).html(data);

                    jQuery("#"+hideform).hide();
                }
            }
     });
}
/* Message length check */
var len = 0;
var msg = 0;
var maxlength_notes = 1000;
function doChange_sms_note(smslen,msgid) {
	if (!len) { len = document.getElementById(smslen); }
	if (!msg) { msg = document.getElementById(msgid); }
	if (msg.value.length > maxlength_notes) { msg.value = msg.value.substring(0, maxlength_notes); }
	len.value = (maxlength_notes - msg.value.length);
}


function add_map_place(){
    var submit = true;

    if(jQuery("#coordinates").val()==''){
        jAlert('Nav norādīta vieta kartē!','Paziņojums');
        submit = false;
    }
    if(jQuery("#place_name").val()==''){
        jAlert('Nav norādīta vietas nosaukums!','Paziņojums');
        submit = false;
    }
    if(jQuery("#place_descr").val()==''){
        jAlert('Nav norādīts īss vietas apraksts!','Paziņojums');
        submit = false;
    }

    if(submit!=false){


    	jQuery.ajax({
            type: "POST",
            url: '/'+language+"/objects/save_place_in_map/",
            data: jQuery('#mapform').serialize(),
            success: function(data){
                window.parent.add_selected_place(data);

            }
        });


    }
}
function add_selected_place(id_object){
        dataString = 'F_ID_OBJECT='+id_object;
    	jQuery.ajax({
            type: "POST",
            url: "/lv/objects/publish_google_map/",
            data: dataString,
            success: function(data){

                show_object_points_in_map(id_object);

                jQuery("#place_in_map").html(data);
            }
        });

    SqueezeBox.close();
}

function show_object_points_in_map(id){

    var dataString = 'F_ID_OBJECT='+id;
	jQuery.ajax({
        type: "POST",
        url: "/catalog/publish_map_coordinates/",
        data: dataString,
        dataType: "xml",
        success: function(xml) {

            jQuery("map", xml).each(function(i){

                var _maptype = jQuery(this).find('maptype').text();
                var _coordinate = jQuery(this).find('coordinate').text();
                var _name = jQuery(this).find('name').text();
                var _descr = jQuery(this).find('descr').text();
                descr = '<b>' +_name+ '</b><br /><div style="width:200px;">' +_descr+ '</div>';

                showAddress(_maptype,_coordinate,_name,descr);

            });


        }
    });


}

function remove_place_from_map(id_map,id_object){

        var dataString = 'F_ID_MAP='+id_map

    	jQuery.ajax({
            type: "POST",
            url: "/lv/objects/delete_place_in_map/",
            data: dataString,
            success: function(data){
                map.clearOverlays();

                add_selected_place(id_object);
                show_object_points_in_map(id_object);
            }
        });
}




function show_points_window(){

    jQuery.post("/login/user_points/", { },
	    function(data) {
            jQuery.modal(jQuery("#modalmessage").html(data));
		}
	);
}

function show_default_points_window(){
    jQuery.post("/login/user_default_points/", { },
	    function(data) {
            jQuery.modal(jQuery("#modalmessage").html(data));
		}
	);
}
function showloginform(){
    jQuery.post("/login/user_register_form/", { },
	    function(data) {
            jQuery.modal(data);
            jQuery('#username').focus();
		}
	);
}

function deleteprofileimage(id){

    jQuery('#profileimage').html('');
    jQuery('#profileimage').addClass('loadingdeleteprofileimage');
    jQuery.post("/userreg/deleteprofileimage/", { id:id },
	    function(data) {
            jQuery('#profileimage').removeClass('loadingdeleteprofileimage');
            alert('Profila attēls dzēsts!');

		}
	);
}

function submit_forum_topic(){

    jQuery('.forumcommentform').hide();
    jQuery('#loadingtopic_form').css('background', '#ffffff url(/images/loader_question.gif) center no-repeat');

    document.newtopicform.submit();
}

function questionsubmit(){
    var inputs = jQuery("#quizform input, textarea");
    jQuery('#question-loading').show();
    var queryString = inputs.serialize();
    var url = '/'+language+'/questionform/';
    jQuery.ajax({
        type: "POST",
        url: url,
        data: queryString,
        success: function(data) {
            jQuery('#questionform').html(data);
            jQuery('#question-loading').hide();
        }
    });
}
function front_questionsubmit(){
    var inputs = jQuery("#quizform input, textarea");
    jQuery('#question-loading').show();
    var queryString = inputs.serialize();
    var url = '/'+language+'/questionform/frontquestionform/';
    jQuery.ajax({
        type: "POST",
        url: url,
        data: queryString,
        success: function(data) {
            jQuery('#front-questionform').html(data);
            jQuery('#question-loading').hide();
        }
    });
}
function showprice(){
    jQuery('#text').val('100Ls :)');
    jQuery('#button').show();
    jQuery('#please-whait').hide();
}

function calculateprice(){

    var inputs = jQuery("#ticketform input, select");
    var queryString = inputs.serialize();
    jQuery('#button').hide();
    jQuery('#please-whait').show();
    var url = '/'+language+'/ticketcalculator/';
    jQuery.ajax({
        type: "POST",
        url: url,
        data: queryString,
        success: function(data) {
            jQuery('#text').val(data);
            jQuery('#button').show();
            jQuery('#please-whait').hide();
            //jQuery('#info-message').show();
        }
    });

}

