
function ajaxShowMap(level,locate,id){
        $('#show_levels').html('<div class="map_including"><img src="images/ajax-loader-big.gif" /></div>');

         $.post("ajax-frontend/",{
            action:'show_levels',
            level: level
         }, function(data) {
            $('#show_levels').html(data);
             if(locate){
                $.scrollTo('#headerbigp', 1000);
                tooltipShow(id,'show');
             }
         });


}

function locateOnMap(level,local_id,id){
    if(level==1 || level==2 || level==3){
         ajaxShowMap(level,true,id);
    }
}

function openLevel(level,type,open_level_by_number){
    var current_level = $('#current_level').val();

    if(level){
        ajaxShowMap(level,false);
    }

    if(!level){
        if(type=='open'){
            /*
            $('#bt_open_level').hide();
            $('#bt_close_level').show();
            if(!open_level_by_number){
                $('#level_' + current_level + '_map').slideDown(1000);
            }
            $('#level_not_open').hide();
            */
            $('#content2').animate({'margin-top': '0px'}, 400, 'linear', function() {
                $('#bt_open_level').hide();
                $('#bt_close_level').show();
                $('#image_map_level_' + current_level).fadeIn(700);
            });
        }else{
            /*
            $('#bt_open_level').show();
            $('#bt_close_level').hide();
            $('#level_' + current_level + '_map').hide();
            $('#level_not_open').show();
            $('#block_show_levels').css('height','auto');
            */
            //content2
            //$('#content2').css('top', '640px').show();
            $('#content2').animate({'margin-top': '-309px'}, 400, 'linear', function() {
                $('#bt_open_level').show();
                $('#bt_close_level').hide();
                $('#image_map_level_' + current_level).fadeOut(300);
            });
            //$('#level_not_open').show();
            //$('#level_' + current_level + '_map').slideUp(1000);
        }
    }else{
        openLevel('','open','1');
        $('#level_' + current_level + '_map').hide();
        $('#current_level').val(level);
        $('#level_' + level + '_map').fadeIn(1000);
        $('#block_show_levels').css('height','388');
    }
    
}
function tooltipShow(id,action){
    if(action=='show'){
        $('.descshop').hide();
        $('#tooltip_' + id).show();
    }else{
        $('#tooltip_' + id).hide();
    }
}
function createCaterorySelector(letter) {
        $('#select_category').change(function(){


        var category_id = $(this).val();

        $('#select_brand').html('<option></option>');
        $('#content').html('<img src="images/ajax-loader.gif" />');

            $.get(
                URL + 'ajax-frontend/',
               'action=load_store_brand&category_id=' + category_id + '&letter=' + letter,
                function (result) {
                    if (result.type == 'error') {

                        return(false);
                    }
                    else {
                        var options = "<option valule='0'></option>";
                        $(result.brand).each(function() {
                            options += '<option value="' + $(this).attr('id') + '">' + $(this).attr('title') + '</option>';
                        });
                        $('#select_brand').html(options);
                        $('#content').html(result.store_category);
                        Cufon.refresh();
                    }
                },
                "json"
            );

            
        });
}
function createBrendSelector(letter){
        $('#select_brand').change(function(){
            
        var category_id = $('#select_category').val();
        var brand_id = $(this).val();

        $('#content').html('<img src="images/ajax-loader.gif" />');

            $.get(
               URL + 'ajax-frontend/',
               'action=view_store_by_brend&category_id='+category_id+'&letter=' + letter + '&brand_id=' + brand_id,
                function (result) {
                    if (result.type == 'error') {
                        return(false);
                    }
                    else {
                        if(!result.store_category){
                            $('#no_search_results').show();
                        }else{
                            $('#no_search_results').hide();
                        }
                        $('#content').html(result.store_category);
                        Cufon.refresh();
                    }
                },
                "json"
                );
             });
}

function putDefaultSearchText(default_text){
    var search_value = $('#searchKeywords').val();
    if(!search_value) {
        $('#searchKeywords').val(default_text);    
    }
}



function searchKeywordsIn(default_text){
    var search_value = $('#searchKeywords').val();
    if(search_value==default_text) {
        $('#searchKeywords').val('');    
    }else{
        //'action=search&word='+ encodeURI(search_value),
        if(search_value.length>2){
            $.get(
               URL + 'ajax-frontend/',
               'action=search&word='+ encodeURI(search_value),
                function (result) {
                    if (result.type == 'error') {
                        return(false);
                    }
                    else {
                        if(!result.store_category){
                            $('#no_search_results').show();
                        }else{
                            $('#no_search_results').hide();
                        }
                        $('#content').html(result.store_category);
                        Cufon.refresh();
                    }
                },
                "json"
                );
             
        }
    }
}

// shops news & events
function moreStoreNews(storeId, scrollBottom) {
    $.post(URL+'ajax/', {action:'load_store_news_and_events', storeId:storeId, start:moreNewsStart}, function(data) {
        $('#newsBlock').append(data);
        if (scrollBottom) {
            $.scrollTo('100%', 1000);
            TagToTip('333', FOLLOWMOUSE, true);
        }
    });
}
function hideStoreMoreNews() {
    $('.pinkdiv').hide();
}

// news & events
function moreNews(scrollBottom) {
    $.post(URL+'ajax/', {action:'load_news_and_events', start:moreNewsStart}, function(data) {
        $('#newsBlock').append(data);
        if (scrollBottom) {
            $.scrollTo('100%', 1000);
        }
    });
}
function hideMoreNews() {
    $('.newbutton').hide();
}
// job posting
function moreJobPosting(scrollBottom) {
    $.post(URL+'ajax/', {action:'load_job_posting', start:moreJobPostingStart, storeId:moreJobPostingStoreMode, moreJobPostingCategory:moreJobPostingCategory}, function(data) {
        $('#jobPostingBlock').append(data);
        if (scrollBottom) {
            $.scrollTo('100%', 1000);
        }
    });
}
function hideMoreJobPosting() {
    $('.pinkdiv').hide();
}
function showMoreJobPosting() {
    $('.pinkdiv').show();
}
function getStoreListByCat(obj) {
    var categoryId = obj.value;
    if (categoryId > 0) {
        moreJobPostingStoreMode = 0;
        
        // load all job posting in current category
        $('#jobPostingBlock').html('');
        moreJobPostingStart = 0;
        moreJobPostingCategory = categoryId;
        moreJobPosting(0);
        
        // fill store list by current category
        var storeObj = document.getElementById('ajaxStoreListByCat');
        storeObj.options.length = 1;
        storeObj.options[0].text = "";
        $.getJSON(URL+'ajax/', {action:'getStoreListByCatId', categoryId: categoryId}, function(data) {
            var statesCount = data.length;
            for (var i = 0; i < statesCount; i++) {
                storeObj.options[storeObj.options.length] = new Option(data[i].name, data[i].id);
            }
            storeObj.options[0].text = "";
            $.uniform.update();
        });
    } else {
        // clear sub list
        var storeObj = document.getElementById('ajaxStoreListByCat');
        storeObj.options.length = 1;
        storeObj.options[0].text = "";
        $.uniform.update();
        // clear clock
        $('#jobPostingBlock').html('');
        moreJobPostingStart = 0;
        moreJobPostingStoreMode = 0;
        moreJobPostingCategory = 0;
        moreJobPosting(0);
    }
}
function getJobPoslingListByStoreId(obj) {
    var storeId = obj.value;
    $('#jobPostingBlock').html('');
    moreJobPostingStart = 0;
    moreJobPostingStoreMode = storeId;
    moreJobPostingCategory = 0;
    moreJobPosting(storeId);
}

function showLeftGreenContent(type,ipad){
    if(type=='hide'){
        $(document).unbind('mousemove');
        $('.greencorner_block').hide(1000, function() {
            $('.greencorner_block .greendiv').fadeOut(1000);
        });
        $('.greencorner_block .greendiv').hide();
        if(ipad){
            $('#leaf_open').show();
            $('#leaf_close').hide();
        }
    }else{
        if(ipad){
            $('#leaf_open').hide();
            $('#leaf_close').show();
        }

        $('.greencorner_block').show(1000, function() {
            $('.greencorner_block .greendiv').fadeIn(1000);
        });
        
        var greenlistObj = $('.greenlist').offset();
        greenlistLeft = greenlistObj.left;
        greenlistTop  = greenlistObj.top;  
        
        var x1 = greenlistLeft - 480;
        var y1 = greenlistTop;
        var x2 = greenlistLeft + 100;
        var y2 = greenlistTop;
        var x3 = greenlistLeft + 100;
        var y3 = greenlistTop + 582;
        
        //mouseover.myEvents
        if(!ipad){
            $(document).bind('mousemove', function(a) {
                var x = a.clientX;
                var y = a.clientY;
                if ( ( (x3 - x) / (x3 - x1) ) - ( (y3 - y) / (y3 - y1) ) > 0 || x > x2 || y < y2) {
                    showLeftGreenContent('hide');
                }
            });
        }
    }
}


// SALES AND PROMOTIONS
function moreSalesAndPromotions(storeId, scrollBottom) {
    $.post(URL+'ajax/', {action:'load_sales_and_promotions', start:moreJobPostingStart, storeId:moreJobPostingStoreMode, moreJobPostingCategory:moreJobPostingCategory}, function(data) {
        $('#jobPostingBlock').append(data);
        if (scrollBottom) {
            $.scrollTo('100%', 1000);
        }
    });
}
function getStoreListByCatForSales(obj) {
    var categoryId = obj.value;
    if (categoryId > 0) {
        moreJobPostingStoreMode = 0;
        
        // load all job posting in current category
        $('#jobPostingBlock').html('');
        moreJobPostingStart = 0;
        moreJobPostingCategory = categoryId;
        moreSalesAndPromotions(0);
        
        // fill store list by current category
        var storeObj = document.getElementById('ajaxStoreListByCat');
        storeObj.options.length = 1;
        storeObj.options[0].text = "Loading...";
        $.getJSON(URL+'ajax/', {action:'getStoreListByCatIdForSales', categoryId: categoryId}, function(data) {
            var statesCount = data.length;
            for (var i = 0; i < statesCount; i++) {
                storeObj.options[storeObj.options.length] = new Option(data[i].name, data[i].id);
            }
            storeObj.options[0].text = "SELECT A BUTIQUE";
            $.uniform.update();
        });
    } else {
        // clear sub list
        var storeObj = document.getElementById('ajaxStoreListByCat');
        storeObj.options.length = 1;
        storeObj.options[0].text = "SELECT A BUTIQUE";
        $.uniform.update();
        // clear clock
        $('#jobPostingBlock').html('');
        moreJobPostingStart = 0;
        moreJobPostingStoreMode = 0;
        moreJobPostingCategory = 0;
        moreSalesAndPromotions(0);
    }
}
function getSalesListByStoreId(obj) {
    var storeId = obj.value;
    $('#jobPostingBlock').html('');
    moreJobPostingStart = 0;
    moreJobPostingStoreMode = storeId;
    moreJobPostingCategory = 0;
    moreSalesAndPromotions(storeId);
}
// NEWSLETTER
function newslettrerBoxShow(type) {

    //$('#newsletterPopup').show("slide", { direction: "down" }, 500);

     //$('#newsletterPopup').animate({left: '640px'}, 1000, 'linear', function() {
      //  $('#newsletterPopup').hide();
    //});
    if(type=='succ'){
        $('#newsl_top_block').hide();
        $('#newsletterPopup .message1').show();
        $('#newsletterPopup form.newsletter').hide();
    }
    if(type=='confirm'){
        $('#newsl_top_block').hide();
        $('#newsletterPopup .message2').show();
        $('#newsletterPopup form.newsletter').hide();
    }
    $('#default_newsltext').show();
    $('#text_confirm_subscribe').hide();
    $('#text_newsletter_succ').hide();
    $('#text_newsletter_error').hide();

    
    $('#newsletterPopup').css('top', '303px').show();
    $('#newsletterPopup').animate({top: '153px'}, 220, 'linear');

}
function newslettrerBoxHide() {
    $('#newsletterPopup .message1').hide();
    $('#newsletterPopup form.newsletter').show();
    $('#newsletter_name').val( $('#newsletter_name').attr('title') ).removeClass('wrong').removeClass('allow');
    $('#newsletter_email').val( $('#newsletter_email').attr('title') ).removeClass('wrong').removeClass('allow');
    $('#newsletterPopup .bluediv').show();
    $('#newsletterPopup .ajax_indicator').hide();
    $('#newsletter_error_name').add('#newsletter_error_email').hide(); // hide warning
    $('#newsletterPopup .black').show(); // show look text
    $('#newsl_top_block').show();
    //$('#newsletterPopup').hide("slide", { direction: "down" }, 500);

    $('#newsletterPopup').animate({top: '303px'}, 220, 'linear', function() {
        $('#newsletterPopup').hide();
    });
}
function fieldFocus(obj) {
    var val     = obj.value;
    var def_val = obj.title;
    if (val == def_val) {obj.value = '';}
}
function fieldBlur(obj) {
    var val     = obj.value;
    var def_val = obj.title;
    if (val == '') {obj.value = def_val;}
}
$(document).ready(function () {
    var options = {
        dataType: 'json',
        success: function(data) {
            $('#newsletterPopup .bluediv').show();
            $('#newsletterPopup .ajax_indicator').hide();
            if (data.status == 'success') {
                //$('#newsletterPopup .message1').show();
                //$('#newsletterPopup form.newsletter').hide();

                $('#newsletters').attr('action','http://optin.cyberimpact.ca/');
               
                var myTextField = document.getElementById('newsletters');
                myTextField.submit();
            } else {
                var alreadyError = 0;
                for (keyField in data.errors) {
                    $('#newsletterPopup #newsletter_' + data.errors[keyField]['field']).removeClass('allow').addClass('wrong');
                    if (alreadyError == 0) {
                        $('#newsletter_error_name').add('#newsletter_error_lastname').add('#newsletter_error_email').add('#newsletter_error_email_dup').hide();
                        $('#newsletterPopup .black').hide();
                        $('#newsletter_error_'+data.errors[keyField]['field']).show();
                        alreadyError = 1;
                    }
                }

                for (keyField in data.succ) {
                    $('#newsletterPopup #newsletter_' + data.succ[keyField]['field']).removeClass('wrong').addClass('allow');
                }
            }
        }, beforeSubmit: function() {
            //$('#newsletterPopup .bluediv').hide();
            //$('#newsletterPopup .ajax_indicator').show();
        }
    };
    $("form.newsletter").ajaxForm(options);


    showGeenList();
});

function openTooltipStore(type,level,local_id,key){
    var current = $('#current_tooltips_' + level + '_' + local_id).val();
    count_all_elements=0;
    $('div#tooltips_' + local_id + ' input#counter_elevents').each(function(index){
       count_all_elements++;
    });
    if(type == 'next'){
        next_block = (current*1)+1;
        var content_block = $('#tooltipBlock_' + level + '_' + local_id + '_' + next_block).html();    
        if(content_block){
            $('#tooltipBlock_' + level + '_' + local_id + '_' + current).hide();
            $('#tooltipBlock_' + level + '_' + local_id + '_' + next_block).show();
            $('#current_tooltips_' + level + '_' + local_id).val((current*1)+1);
        }else{
            $('#tooltipBlock_' + level + '_' + local_id + '_' + current).hide();
            $('#tooltipBlock_' + level + '_' + local_id + '_1').show();
            $('#current_tooltips_' + level + '_' + local_id).val(1);
        }
    }else if(type == 'prev'){
        prev_block = (current*1)-1;
        var content_block = $('#tooltipBlock_' + level + '_' + local_id + '_' + prev_block).html();
        if(content_block){
            $('#tooltipBlock_' + level + '_' + local_id + '_' + current).hide();
            $('#tooltipBlock_' + level + '_' + local_id + '_' + prev_block).show();
            $('#current_tooltips_' + level + '_' + local_id).val((current*1)-1);
        }else{
            $('#tooltipBlock_' + level + '_' + local_id + '_' + current).hide();
            $('#tooltipBlock_' + level + '_' + local_id + '_' + count_all_elements).show();
            $('#current_tooltips_' + level + '_' + local_id).val(count_all_elements);
        }
    }else if(type == 'curent'){
        $('#tooltipBlock_' + level + '_' + local_id + '_' + current).hide();
         $('#tooltipBlock_' + level + '_' + local_id + '_' + key).show();
         $('#current_tooltips_' + level + '_' + local_id).val(key);
    }
}

// NEWSLETTER
// GIFT CARD
function giftCardBlockShow() {
    $('.gift_card_box').css('left', '640px').show();
    $('.gift_card_box').animate({left: '0px'}, 400, 'linear');
    //$('.gift_card_box').show("slide", { direction: "right" }, 1000);
}
function giftCardBlockHide() {
    //$('.gift_card_box').hide("slide", { direction: "right" }, 1000);
    $('.gift_card_box').animate({left: '640px'}, 400, 'linear', function() {
        $('.gift_card_box').hide();
    });
}
// GIFT CARD
// GREEN AND RESPONSIBLE
function showGreenBlock(id) {
    var state = $('#gift_card_box_'+id).attr('used');
    if (state) {
        $('#green_li_'+id+' li').removeClass('arrow2').addClass('arrow');
        $('#green_li_'+id+' li a').css('color', '#838383');
        $('#gift_card_box_'+id).attr('used', '');
    } else {
        $('#green_li_'+id+' li').removeClass('arrow').addClass('arrow2');
        $('#green_li_'+id+' li a').css('color', '#008F21');
        $('#gift_card_box_'+id).attr('used', 'on');
    }
    $('#gift_card_box_'+id).slideToggle();
}

   $(document).ready (function(){
        $(".form-item select").uniform();

	    $("#back-top").hide();
        $(window).scroll(function(){
            if($(this).scrollTop() > 100)
            {
                $('#back-top').fadeIn();
            }
            else
            {
                $('#back-top').fadeOut();
            }
        });
        $('#back-top a').click(function(e){
            e.preventDefault();
            $('body,html').animate({
                scrollTop: 0
                },
                500,
                function(){
                });
        });
        if(!not_show_random_bg){
            initializeBackgroundImage(bg, true);
        }
    });


var imageEl;
var gradientsEl;
var showGradients;
function initializeBackgroundImage(src, gradients) {
    showGradients = gradients;
    imageEl = jQuery('<img>').attr('src', src).addClass('background-image');
    a = jQuery('<div>').addClass('background-image-wrapper').append(imageEl).prependTo(document.body);

    if (jQuery.browser.msie && jQuery.browser.version.substr(0,1) < 7) {
        imageEl.attr('galleryimg', 'yes');
        jQuery(document.body).css("height", "100%");
    }
    if (showGradients) {
        gradientsEl = jQuery('<div>').addClass('gradients').html('<div class="top"></div><div class="bottom"></div>').appendTo(a);
    }
    window.onresize = setImagePosition;
    setImagePosition();
}
function setImagePosition() {
    var a = {x:0,y:0};
    var c = {x:0,y:0};
    a.x = jQuery(window).width();
    a.y = jQuery(window).height();

    c.x = jQuery(imageEl).width();
    c.y = jQuery(imageEl).height();

    if (c.x === 0) {
        //c = {x:1920,y:1200}
        c = {x:2541,y:1687}
    }
    var b;
    if (a.x*(c.y/c.x)>a.y) {
        b=a.x/c.x
    } else {
        b=a.y/c.y
    }
    var e=Math.round(c.x*b);
    var d=Math.round(c.y*b);

    jQuery(imageEl).attr('width', e).attr('height', d).css({left:(a.x-e)/2,top:(a.y-d)/2});
    if (showGradients) {
        jQuery(gradientsEl).css({width:a.x,height:a.y});
    }

}

function showInteractiveVideo(filelist,swf_1,swf_2,lang){
    var flashvars = {
        fileList:filelist,
        language:lang,
        formweb: URL + "save-subscribe/",
        tracking:  URL + "ajax-frontend/?action=save_int_results"
    };
    var params = {
        menu: "false",
        scale: "noScale",
        allowFullscreen: "true",
        allowScriptAccess: "always",
        bgcolor: "#000000",
        wmode: "opaque"
    };
    var attributes = {
        id:"Rockland"
    };
    swfobject.embedSWF(swf_1, "altContent", "958", "540", "9.0.0", swf_2, flashvars, params, attributes);
}
function showGeenList(){
    var flashvars = {};
    var params = {
        wmode: "transparent",
        scale: "noScale",
    };
    var attributes = {};

    swfobject.embedSWF("swf/leaf.swf", "greenlist", "100", "120", "9.0.0","",flashvars,params,attributes);
}
function createFrame1(){
    var flashvars = {};
    var params = {
        scale: "noScale",
        allowFullscreen: "false",
        allowScriptAccess: "always",
        bgcolor: "#000000",
        wmode: "opaque",
        play: "true"
    };
    var attributes = {};

    swfobject.embedSWF("swf/blocks/open/day2.flv", "frame_1", "319", "242", "9.0.0","",flashvars,params,attributes);
}
function couponShow(id) {
    $('#coupon_'+id).show();
    $('#coupon_'+id).bind('mouseleave', function(e) {
        couponHide(id);
    });
}
function couponHide(id) {
    $('#coupon_'+id).hide();
    $('#coupon_'+id).unbind('mouseleave');
}
function couponPrint(imgUrl, title) {
    var printContentHtml = '';
    printContentHtml += '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">';
    printContentHtml += '<html>';
    printContentHtml += '<head>';
    printContentHtml += '<title>'+title+'</title>';
    printContentHtml += '<meta http-equiv="Content-type" content="text/html; charset=utf-8">';
    printContentHtml += '</head>';
    printContentHtml += '<body>';
    printContentHtml += '<img width="587" height="173" src="'+imgUrl+'">';
    printContentHtml += '</body>';
    printContentHtml += '</html>';
    
    printIframe.document.clear();
    printIframe.document.open();
    printIframe.document.write(printContentHtml);
    printIframe.document.close();
    
    frames["printIframe"].focus();
    frames["printIframe"].print();

}
function movePage(url) {
    document.location.href = url;
}
function movePageNew(url) {
    window.open(url);
}
function popupRules(type) {

    $.post(URL + 'ajax-frontend/', {action:'get_rules_text'}, function(data) {
        $('.rules_popup_text').html(data);
    });

    if(type=='close'){
        $('#rulesPopup').fadeOut(1000);
    }
    if(type=='open'){
        $('#rulesPopup').fadeIn(1000);
    }
}
