﻿            //Functions used on the Default Page            
            $(document).ready(function() {
           
                //New Login handler
                hook_top_actions();
            
                //Fill in the site items
                displaySubCats("","1");
                //top10('Most Popular Downloads');
                top10('Featured Tracks');
                getBasketTotal();
                
                //Setup the click functions;
                $("#searchbutton").click(function() {
                    runSearch();
                    inBasket = false;
                });
                
                $("#home_button").click(function() {
                    $("#searchdl").val("");
                    displayAlbum("The Latest downloads","","","");
                    displaySubCats("","1");
                    inBasket = false;
                    return false;
                });
                $(".maincat_button").click(function() {
                    $("#searchdl").val("");
                    displayAlbum("View all " + $(this).attr('rel'),0,$(this).attr('rel'),"");
                    displaySubCats($(this).attr('rel'),"0");
                    inBasket = false;
                    return false;
                });
                $(".subcats").live("click", function(e) {
                    $("#searchdl").val("");
                    displayAlbum("View all " + $(this).attr('rel') + " / " + $(this).attr('cat'),0,$(this).attr('rel'),$(this).attr('cat'));
                    inBasket = false;
                    return false;
                });
                $(".dlbutcheck").click(function() {
                    show_checkout();
                    //alert('Checkout will be enabled shortly');
                    return false;
                });
            });
                         
            function captureKeys(evt) {
               var c = document.layers ? evt.which
                        : document.all ? event.keyCode
                        : evt.keyCode;
                if (c == 13) {
                    runSearch();
                    return true;
                }
            }
                    
            function runSearch() {
                $.ajax({
                    type: "POST",
                    url: "/idream/albumcontents.asp",
                    data: ({
                        albumTitle: "Search Results: " + $("#searchdl").val(),
                        keywords: $("#searchdl").val(),
                        jumpPage: 0
                    }),
                    success: function(msg) {
                        if ($("#searchdl").val() != '') {
                            $.growlUI('','Searching for "' + $("#searchdl").val() + '"...');
                        }
                        $(".dlbot").html(msg);
                        $.scrollTo($(".dlbot"));
                    }
                });
                inBasket = false;
            }
            
            function displayAlbum(albumTitle,albumID,albumType,albumSubType) {
                //Change the title of the box
                $(".dlbottop2").html('');
                //Call a script to pull out the contents based on the albumID
                $.ajax({
                    type: "POST",
                    url: "/idream/albumcontents.asp",
                    data: ({
                        albumTitle: albumTitle,
                        albumID: albumID,
                        albumType: albumType,
                        albumSubType: albumSubType,
                        jumpPage: 0
                    }),
                    success: function(msg) {
                        $(".dlbot").html(msg);
                    }
                });
                $(".bottomtitle").html(albumTitle);
                var so = new SWFObject("/idream/download_player/3dcarouselv2dev.swf", "3dcarouselv2dev", "500", "245", "9.0.115.0", "#ffffff");
                so.addParam("allowSciptAccess", "always");
                so.addParam("quality", "high");
                so.addParam("scale", "noscale");
                so.addParam("menu", "false");
                so.addParam("salign", "lt");
                so.addParam("wmode", "transparent");
                so.addParam("flashvars", "configfile=/idream/genericcarousel.asp?type=" + albumType);
                so.write("flashcontent");
                
                inBasket = false;
            }
            
            
            function displayBasketContents() {
                //Change the title of the box
                $(".dlbottop").html("iDream - Downloads - Shopping Basket");
                $(".dlbottop2").html("You can purchase these items by checking out now");
                //Call a script to pull out the contents based on the albumID
                $.ajax({
                    type: "POST",
                    url: "/idream/getbasketinfo.asp",
                    data: ({
                        itemString: itemString,
                        elementReturn: "mainBasket"
                    }),
                    success: function(msg) {
                        $(".dlbot").html(msg);
                        $.scrollTo($(".dlbot"));
                    }
                });
                inBasket = true;
                return false;
            }

            function showCheckout() {
                //Change the title of the box
                $(".dlbottop").html("iDream - Downloads - Checkout");
                $(".dlbottop2").html("Checkout on iDream");
                //Call a script to pull out the contents based on the albumID
                $.ajax({
                    type: "POST",
                    url: "/idream/checkout.asp",
                    data: ({
                        itemString: itemString,
                        elementReturn: "mainBasket"
                    }),
                    success: function(msg) {
                        $(".dlbot").html(msg);
                        $.scrollTo($(".dlbot"));
                    }
                });
                inBasket = true;
                return false;
            }
                    
            function displaySubCats(subCatName,isFeatured) {
                //Change the title of the box
                //Call a script to pull out the contents based on the albumID
                $.ajax({
                    type: "POST",
                    url: "/idream/subcatinfo.asp",
                    data: ({
                        subCatName: subCatName,
                        isFeatured: isFeatured
                    }),
                    success: function(msg) {
                        $(".dlleft2internal").html(msg);
                    }
                });
            }
                   
            function top10(top10Title) {
                //Call a script to pull out the contents based on the albumID
                $.ajax({
                    type: "POST",
                    url: "/idream/top10list.asp",
                    data: ({
                        dlchoicetitle: top10Title
                    }),
                    success: function(msg) {
                        $(".dlchoice").html(msg);
                    }
                });
            }        
            
            //Lightbox opening for photo samples
            function openAlbumCoverInfo(sampleTitle, sampleDesc, albumCover) {
                msg="<h2 style=\"margin-bottom:10px\" >" + sampleTitle + "</h2><img src=\"" + albumCover + "\" alt=\"" + sampleTitle + "\" /><p style=\"margin:10px\">" + sampleDesc + "</p>";
                $.blockUI({
                    message: '<div>' + msg + '</div>',
                    css: { width: '450px', minheight: '250px', padding: '5px', top: '15%', left: '35%', overflow: 'hidden', border: 'solid 3px black' }
                });
                $('.blockOverlay').attr('title', 'Close this window').click($.unblockUI);
                $('a.media').media();
                inBasket = false;
                return false;
            }

            //Lightbox opening for photo samples
            function openPreview(itemPreview) {
                msg="<img src='" + itemPreview + "' height='600' alt='Preview image'>";
                $.blockUI({
                    message: '<div>' + msg + '</div>',
                    css: { width: '500px', padding: '5px 10px', top: '5%', left: '32%', overflow: 'hidden', border: 'solid 3px black' }
                });
                $('.blockOverlay').attr('title', 'Close this window').click($.unblockUI);
                $('a.media').media();
                inBasket = false;
                return false;
            }
            
            //Lightbox opening for photo samples
            function openPreviewPDF(itemPreview) {
                msg="<iframe src='" + itemPreview + "' width='100%' style='height:600px' z-index='-100'></iframe>";
                $.blockUI({
                    message: '<div>' + msg + '</div>',
                    css: { width: '600px', padding: '5px 10px', top: '5%', left: '32%', overflow: 'hidden', border: 'solid 3px black' }
                });
                $('.blockOverlay').attr('title', 'Close this window').click($.unblockUI);
                $('a.media').media();
                inBasket = false;
                return false;
            }
            
            //If you click on an item then display the info page (slightly more about the item)
            function displayInfoPage(albumType,itemID) {
                //Call a script to pull out the contents based on the albumID
                $.ajax({
                    type: "POST",
                    url: "/idream/iteminfopage.asp",
                    data: ({
                        itemID: itemID
                    }),
                    success: function(msg) {
                        $(".dlbot").html(msg);
                        $.scrollTo($(".dlbot"));
                    }
                });
                //Change the title of the box
                $(".dlbottop").html('Loading download information...');
                $(".dlbottop2").html('');
                inBasket = false;

                var so = new SWFObject("/idream/download_player/3dcarouselv2dev.swf", "3dcarouselv2dev", "500", "245", "9.0.115.0", "#ffffff");
                so.addParam("allowSciptAccess", "always");
                so.addParam("quality", "high");
                so.addParam("scale", "noscale");
                so.addParam("menu", "false");
                so.addParam("salign", "lt");
                so.addParam("wmode", "transparent");
                so.addParam("flashvars", "configfile=/idream/genericcarousel.asp?type=" + albumType);
                so.write("flashcontent");
                
                return false;
            }
            
            //Add an item to the basket, if it's not already in there.
            function addToBasket(itemID) {
                itemString = new String($.cookie('download_items'));
                //Get the current Cookie
                if ( itemString == 'null' ){
                    itemString = itemID;
                    $.growlUI('Basket', 'Item has been added!');
                } else {
                    checkString = new String(";" + itemString + ";")
                    //Add the new item to the Cookie if it's not already in there
                    if (parseInt(checkString.indexOf(";"+itemID+";")) < 0 ) {
                        itemString = itemString + ';' + itemID;
                        $.growlUI('Basket', 'Item has been added!');
                    } else {
                        $.growlUI('Basket', 'Already in basket!');
                    }
                }
                $.cookie('download_items',itemString)
                
                //Rewrite the totals
                getBasketTotal();
                if ( inBasket ) {
                    displayBasketContents();
                }
                return false;
            }
            
            
            //Add an item to the basket, if it's not already in there.
            function removeFromBasket(itemID) {
                itemString = new String($.cookie('download_items'));
                //Get the current Cookie
                if ( itemString == 'null' ){
                    $.growlUI('Basket', 'Basket empty');
                } else {
                    checkString = new String(";" + itemString + ";");
                    //Add the new item to the Cookie if it's not already in there
                    if (parseInt(checkString.indexOf(";"+itemID+";")) >= 0 ) {
                        newItemString = new String(checkString);
                        newItemString = newItemString.replace(";"+itemID,"");
                        if (parseInt(String(newItemString).length) > 0 ) {
                            newItemString = String(newItemString).substring(0,String(newItemString).length-1);
                        } 
                        $.growlUI('Basket', 'Item has been removed!');
                        newItemString = newItemString.replace(";;",";");
                        //Rewrite the totals
                        $.cookie('download_items',newItemString);
                        getBasketTotal();
                        displayBasketContents();
                    }
                }
                return false;
            }
            
            //Write the totals by passing the IDs to an external page and pulling through the prices
            function getBasketTotal() {
                itemString = new String($.cookie('download_items'));
                //$(".dlshoptext").html('Loading...');
                $.ajax({
                    type: "POST",
                    url: "/idream/getbasketinfo.asp",
                    data: ({
                        itemString: itemString,
                        elementReturn: "shortBasket"
                    }),
                    success: function(msg) {
                        $(".dlshoptext").html(msg);
                    }
                });
                return false;
            }
            
            //Write the totals by passing the IDs to an external page and pulling through the prices
            function emptyBasket() {
                if ( confirm('Are you sure you want to empty your basket?') ) { 
                    $.cookie('download_items','')
                    $.growlUI('Basket', 'Basket Emptied');
                    //Rewrite the totals
                    getBasketTotal();
                }
                if ( inBasket ) {
                    displayBasketContents();
                }
                return false;
            }
            
            
            function addyourdownload(obj) {
                return show_download_manager();
                var sUrl = $(obj).attr('rel');
                if (lgUID > 0 ) {
                    $.ajax({
                        type: "GET",
                        url: sUrl,
                        success: function(msg) {
                            $.blockUI({
                                message: '<div>' + msg + '</div>',
                                css: { width: '620px', height: '500px', padding: '5px', top: '20%', left: '30%', cursor: 'default', border: 'solid 3px black' }
                            });
                            $('.blockOverlay').attr('title', 'Close this window').click($.unblockUI);
                        }
                    });
                } else {
                    if (confirm('You must be have an account to sell your items online')) {
                        document.location.href='/login.asp?strReturnPage=/idream/?sellitems=y';
                    } else {
                        return false;
                    }
                }
            }    
            
            function displayAddAlbum(AlbumID,AlbumTitle) {
                $("#step3title").html("Step 3 - Add / Edit the album - " + AlbumTitle);
                $("#step3addtracks").show();
                $("#dlitemframe").html("<div class=\"downloadadd_title\">Current download items</div><iframe src=\"/idream/selectitems.asp?alb=" + AlbumID + "\" id=\"newItemFrame\" name=\"newItemFrame\" frameborder=\"0\"></iframe>");            
                document.myNewItemForm.D_AlbumID.value=AlbumID;
            } 
            
