$(document).ready(function(){
    MM_preloadImages('images/n_about_over.jpg','images/n_advertise_over.jpg','images/n_latest_add_over.jpg','images/n_deals_over.jpg','images/n_advert_cartegories_over.jpg','images/n_feedback_over.jpg','images/n_contact_over.jpg');

/*setInterval(function() {
        slideshowH('r');
    }, 6000);*/
});

function MM_swapImgRestore() { //v3.0
    var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
    var d=document; if(d.images){
        if(!d.MM_p) d.MM_p=new Array();
        var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
            if (a[i].indexOf("#")!=0){
                d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];
            }
    }
}

function MM_findObj(n, d) { //v4.01
    var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
        d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);
    }
    if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
    for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
    if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
    var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
        if ((x=MM_findObj(a[i]))!=null){
            document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];
        }
}

function homeBanner(direction){
    var frame_width = 461;
    var beltposition = $("#image_belt").position();
    var leftpx = beltposition.left;

    var beltwidth = $("#image_belt").width();
    //alert('width '+beltwidth+' left '+leftpx);
    var new_left_position = 0;

    if(direction == 'p'){
        if(Math.abs(leftpx) == 0) new_left_position = (beltwidth - frame_width) * -1;
        else new_left_position = parseInt(leftpx) + frame_width;
    }
    else if(direction == 'n'){
        if((beltwidth - frame_width) == Math.abs(leftpx)) new_left_position = 0;
        else new_left_position = parseInt(leftpx) - frame_width;
    }

    $("#image_belt").css('left', new_left_position+'px');
}

function val_login(){
    if($('#username').val()==''){
        alert('User name cannot be empty');
        $('#username').focus();
        return false;
    }
    if($('#password').val()==''){
        alert('Password cannot be empty');
        $('#password').focus();
        return false;
    }
    return true;
}

function slideshowH(direction){
    var images = $('#strimg').val();
    var countimg = $('#totimg').val();
    var currimg = $('#curimg').val();

    if(countimg > 0){
        var arr_images = images.split(":");

        var nextimg = 0;
        if(direction == 'p'){
            nextimg = parseInt(currimg) - 1;
            if(nextimg == -1) nextimg = parseInt(countimg - 1);
        }
        else if(direction == 'n'){
            nextimg = parseInt(currimg) + 1;
            if(nextimg == countimg) nextimg = 0;
        }
        else if(direction == 'r'){
            var i = arr_images.length;
            nextimg = Math.floor(i*Math.random());
        /*var n_id = arr_iumages[rnd_no];
            window.location.href ="javascript:in_focus("+n_id+");";*/
        }

        //$('#this_week_adimage').attr('src' , 'includes/frontimage.php?cmd=img&imgid='+arr_images[nextimg]);
        $('#this_week_adimage').attr('src' , 'advertise/events/'+arr_images[nextimg]);

        var url = "#";
        var post = 'cmd=url&imgid=' + arr_images[nextimg];
        $.post('includes/frontimage.php', post, function(html){
            url = 'http://' + html;
            $('#this_week_adurl').attr('href', url);
            $('#this_week_adimage').attr('title' , url);
        });
        /*var inner_html = '<a href="http://'+image_url+'" target="_blank">';
        
        post = '';
        $.post('includes/frontimage.php', post, function(){
            
        });
        //var img_path = 'images/101/' + arr_iumages[nextimg];

        $('#real_img').attr('src', img_path);*/
        $('#curimg').val(nextimg);
        
    }
}

function addToBookmark(url, title){
    if (window.sidebar) // firefox
        window.sidebar.addPanel(title, url, "");
    else if(window.opera && window.print){ // opera
        var elem = document.createElement('a');
        elem.setAttribute('href',url);
        elem.setAttribute('title',title);
        elem.setAttribute('rel','sidebar');
        elem.click();
    }
    else if(document.all)// ie
        window.external.AddFavorite(url, title);
}

function slideAutoSwitch() {
    var $active = $('#slideshow A.active');

    if ( $active.length == 0 ) $active = $('#slideshow A:last');

    var $next =  $active.next().length ? $active.next()
    : $('#slideshow A:first');

    $active.addClass('last-active');

    $next.css({
        opacity: 0.0
    })
    .addClass('active')
    .animate({
        opacity: 1.0
    }, 1000, function() {
        $active.removeClass('active last-active');
    });
}

function slideSwitch(dir) {
    var $active = $('#slideshow A.active');
    if ($active.length == 0) $active = $('#slideshow A:first');
    var $next = null;
    if(dir == 'n'){
        $next =  $active.next().length ? $active.next() : $('#slideshow A:last');
    }
    else if(dir == 'p'){
        $next =  $active.prev().length ? $active.prev() : $('#slideshow A:first');
    }

    $active.addClass('last-active');

    $next.css({
        opacity: 0.0
    })
    .addClass('active')
    .animate({
        opacity: 1.0
    }, 1000, function() {
        $active.removeClass('active last-active');
    });
}