/* Change text in input fields
****************************************************/
var shareFriendName = 'Ditt navn'; //Tips venn
var shareFriendMail = 'Mottakers e-post'; //Tips venn
var industryNewsFirstname = 'Fornavn'; //Bransjenyheter
var industryNewsLastname = 'Etternavn'; //Bransjenyheter
var industryNewsAge = 'Alder'; //Bransjenyheter
var industryNewsEmail = 'E-post'; //Bransjenyheter
var industryNewsDescription = 'Beskrivelse (maks 300 tegn)'; //Bransjenyheter


//Max character length for textareas
/***********************************************
* Textarea Maxlength script- © Dynamic Drive (www.dynamicdrive.com)
* This notice must stay intact for legal use.
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/
function ismaxlength(obj, maxLength)
{
    //var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : "";
    if (obj.getAttribute && obj.value.length>maxLength)
        obj.value=obj.value.substring(0,maxLength);
}


/********* Methods and variables for main menu *********/
var timeout    = 500;
var closetimer = 0;
var ddmenuitem = 0;

/*
function menu_open() {
    menu_canceltimer();
    menu_close();
    ddmenuitem = $(this).find("ul").css("visibility", "visible");
}*/

function menu_close() {
	if(ddmenuitem)
	{
	    //close menu
	    ddmenuitem.css("visibility", "hidden");
	    
	    //Change color of parent back to normal when menu is closed
	    $(".mainMenu > li > ul").parent().find("a#142").removeClass("mainMenuActive");
	}
}

function menu_timer() {
	closetimer = window.setTimeout(menu_close, timeout);
}

function menu_canceltimer() {
	if(closetimer) {
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}
/********* Methods and variables for main menu *********/


$(document).ready(function() {

    /********* jQuery - Uploadify *********/
    //Changes style of standard file upload for "Bransjenyheter" in sidebar
    /*
    var id = "55";
    var theString = "asdf";
    $('#ctl00_fileuploadIndustryNewsImage').uploadify({
        'uploader': 'JS/jquery.uploadify/uploadify.swf',
        'script': 'JS/jquery.uploadify/upload.ashx',
        'scriptData': { },
        'cancelImg': 'JS/jquery.uploadify/cancel.png',
        'auto': true,
        'multi': false,
        'fileDesc': 'Image Files',
        'fileExt': '*.jpg;*.png;*.gif;*.bmp;*.jpeg',
        'queueSizeLimit': 5,
        'sizeLimit': 4000000,
        'buttonText': 'Last opp bilde',
        'folder': 'FileUpload/tmp/',
        'onAllComplete': function(event, queueID, fileObj, response, data) {}
    });*/
    /********* jQuery - Uploadify *********/



    // Move poll anwers inside the poll div
    $("#ctl00_repeaterTextboxesRight_ctl01_ContentBoxPlaceHolder_box11582_pnlPoll").appendTo($("#rightboxNarrowPoll"));
    //Create custom input for checkbox and radiobuttons
    $('input').customInput();
    

    //Tips venn input name
    $('#ctl00_ContentPlaceHolder1_txtNavn').attr("value", shareFriendName).css('color', '#8d8e8d').focus(function() {
        if (shareFriendName == $(this).attr("value")) {
            $(this).attr("value", "").css('color', '#000000');
        }
    }).blur(function(){
        if ($(this).attr("value") == '') {
            $(this).attr("value", shareFriendName).css('color', '#8d8e8d');
        }
    });
    
    //Tips venn input mail
    $('#ctl00_ContentPlaceHolder1_txtEpost').attr("value", shareFriendMail).css('color', '#8d8e8d').focus(function() {
        if (shareFriendMail == $(this).attr("value")) {
            $(this).attr("value", "").css('color', '#000000');
        }
    }).blur(function(){
        if ($(this).attr("value") == '') {
            $(this).attr("value", shareFriendMail).css('color', '#8d8e8d');
        }
    });



    //Bransjenyheter input firstname
    $('#ctl00_txtIndustryNewsFirstname').attr("value", industryNewsFirstname).css('color', '#8d8e8d').focus(function() {
        if (industryNewsFirstname == $(this).attr("value")) {
            $(this).attr("value", "").css('color', '#000000');
        }
    }).blur(function(){
        if ($(this).attr("value") == '') {
            $(this).attr("value", industryNewsFirstname).css('color', '#8d8e8d');
        }
    });
    //Bransjenyheter input lastname
    $('#ctl00_txtIndustryNewsLastname').attr("value", industryNewsLastname).css('color', '#8d8e8d').focus(function() {
        if (industryNewsLastname == $(this).attr("value")) {
            $(this).attr("value", "").css('color', '#000000');
        }
    }).blur(function(){
        if ($(this).attr("value") == '') {
            $(this).attr("value", industryNewsLastname).css('color', '#8d8e8d');
        }
    });
    //Bransjenyheter input age
    $('#ctl00_txtIndustryNewsAge').attr("value", industryNewsAge).css('color', '#8d8e8d').focus(function() {
        if (industryNewsAge == $(this).attr("value")) {
            $(this).attr("value", "").css('color', '#000000');
        }
    }).blur(function(){
        if ($(this).attr("value") == '') {
            $(this).attr("value", industryNewsAge).css('color', '#8d8e8d');
        }
    });
    //Bransjenyheter input email
    $('#ctl00_txtIndustryNewsEmail').attr("value", industryNewsEmail).css('color', '#8d8e8d').focus(function() {
        if (industryNewsEmail == $(this).attr("value")) {
            $(this).attr("value", "").css('color', '#000000');
        }
    }).blur(function(){
        if ($(this).attr("value") == '') {
            $(this).attr("value", industryNewsEmail).css('color', '#8d8e8d');
        }
    });
    //Bransjenyheter input description
    $('#ctl00_txtIndustryNewsDescription').attr("value", industryNewsDescription).css('color', '#8d8e8d').focus(function() {
        if (industryNewsDescription == $(this).attr("value")) {
            $(this).attr("value", "").css('color', '#000000');
        }
    }).blur(function(){
        if ($(this).attr("value") == '') {
            $(this).attr("value", industryNewsDescription).css('color', '#8d8e8d');
        }
    });



    /********* Autoscroll of job positions *********/
    $("#scroller").simplyScroll({
        horizontal: false,
        frameRate: 20,
        speed: 1,
        autoMode: 'loop'
    });
    /********* Autoscroll of job positions *********/


    /********* Main Menu *********/
    //Open menu on click
    $(".mainMenu > li").click(function() {
        //menu_open();
        menu_canceltimer();
        menu_close();
        ddmenuitem = $(this).find("ul").css("visibility", "visible");
    });

    //Close menu when mouse leaves
    $(".mainMenu > li").bind("mouseleave", menu_timer);
    
    //Change color of parent when menu is open
    $(".mainMenu > li > ul > li").mouseenter(function() {
        //$(".mainMenu > li > ul").parent().addClass("mainMenuActive");
        //$(".mainMenu > li > ul").parent().find("a#142").addClass("mainMenuActive");
    });
    /********* Main Menu *********/



    /********* Fancybox for image gallery *********/
    function fancyboxStart() { $('embed, object, select').css('visibility', 'hidden'); }
    function fancyboxEnd() { $('embed, object, select').css({ 'visibility' : 'visible' }); }
    $("a.grouped_elements").fancybox({
        'onStart': fancyboxStart,
        'onClosed': fancyboxEnd,
        'titlePosition': 'inside',
        'speedIn': 600,
        'speedOut': 200,
        'overlayOpacity': 1,
        'overlayColor' : '#2d2d2d',
        'titleFormat' : function(title, currentArray, currentIndex, currentOpts) {
				return '<div style="font-weight:bold;padding-right:100px;min-height:9px;">' + this.title + '</div><div style="position:absolute;top:10px;right:10px;color:#666666;">' +  (currentIndex + 1) + '/' + currentArray.length + '</div>';
				}
    });
    /********* Fancybox for image gallery *********/
    
    
    
    /********* Opens a dialogbox to provide input for "Tips oss" in the right column *********/
    $("#rightboxTipsIcon").click(function() {
        $("#dialogTips").dialog({
            height: 260,
            width: 400,
            // Fix: Move dialog inside of Form to ensure PostBack
            open: function(type, data) {
                $(this).parent().appendTo("form");
            }
        });
        return false;
    });
    $("#rightboxTipsText").click(function() {
        $("#dialogTips").dialog({
            height: 260,
            width: 400,
            // Fix: Move dialog inside of Form to ensure PostBack
            open: function(type, data) {
                $(this).parent().appendTo("form");
            }
        });
        return false;
    });
    /********* Opens a dialogbox to provide input for "Tips oss" in the right column *********/



    /********* Opens a dialogbox to provide input for "bransjenyheter" in the right column *********/
    $("#lnkRightBoxIndustryNews").click(function() {
        $("#dialogIndustryNews").dialog({
            height: 320,
            width: 390,
            // Fix: Move dialog inside of Form to ensure PostBack
            open: function(type, data) {
                $(this).parent().appendTo("form");
            }
        });
        return false;
    });
    /********* Opens a dialogbox to provide input for "bransjenyheter" in the right column *********/


    /********* Opens a dialogbox to provide input for "prosjektnytt" in the right column *********/
    $("#lnkRightBoxNewProjects").click(function() {
        $("#dialogNewProjects").dialog({
            height: 360,
            width: 390,
            // Fix: Move dialog inside of Form to ensure PostBack
            open: function(type, data) {
                $(this).parent().appendTo("form");
            }
        });
        return false;
    });
    /********* Opens a dialogbox to provide input for "prosjektnytt" in the right column *********/


    /********* Opens a dialogbox to provide e-mail when clicking on Newsletter *********/
    /*
    $("#headerSearchLinkNewsletter").click(function() {
        $("#dialog").dialog({
            height: 130,
            width: 400,
            // Fix: Move dialog inside of Form to ensure PostBack
            open: function(type, data) {
                $(this).parent().appendTo("form");
            }
        });
        return false;
    });
    
    $("#rightboxNewsletterIcon").click(function() {
        $("#dialog").dialog({
            height: 130,
            width: 400,
            // Fix: Move dialog inside of Form to ensure PostBack
            open: function(type, data) {
                $(this).parent().appendTo("form");
            }
        });
        return false;
    });
    
    $("#rightboxNewsletterText").click(function() {
        $("#dialog").dialog({
            height: 130,
            width: 400,
            // Fix: Move dialog inside of Form to ensure PostBack
            open: function(type, data) {
                $(this).parent().appendTo("form");
            }
        });
        return false;
    });

    $("#footerNewsletter").click(function() {
        $("#dialog").dialog({
            height: 130,
            width: 400,
            // Fix: Move dialog inside of Form to ensure PostBack
            open: function(type, data) {
                $(this).parent().appendTo("form");
            }
        });
        return false;
    });

    $("#articlePageNewsletter").click(function() {
        $("#dialog").dialog({
            height: 130,
            width: 400,
            // Fix: Move dialog inside of Form to ensure PostBack
            open: function(type, data) {
                $(this).parent().appendTo("form");
            }
        });
        return false;
    });

    //    $("#ctl00_btnheadernewsletter").click(function() {
    //        $("#dialog").dialog("close");
    //        return false;
    //    });
    */
    /********* Opens a dialogbox to provide e-mail when clicking on Newsletter *********/
    
    
    /********* Opens a dialogbox for reporting an unapropriate somment *********/
    $(".lnkCommentReportDialog").click(function() {
        var value = "test"; //$(this).attr("rel").val(); //get value from link
        $("input:hidden#commentId").val(value); //set value to hidden field
        $("#commentReportDialog").dialog({
            height: 240,
            width: 390,
            // Fix: Move dialog inside of Form to ensure PostBack
            open: function(type, data) {
                $(this).parent().appendTo("form");
            }
        });
        return false;
    });
    /********* Opens a dialogbox for reporting an unapropriate somment *********/
    
    
    /********* Opens a dialogbox displaying rules for commenting on articles *********/
    $("#lnkCommentRules").click(function() {
        $("#commentRulesDialog").dialog({
            height: 380,
            width: 550,
        });
        return false;
    });
    /********* Opens a dialogbox displaying rules for commenting on articles *********/
    
    
    
    /********* Opens a dialogbox displaying how to set the site as homepage *********/
    $("#lnkSetAsHomepage").click(function() {
        $("#dialogHomepage").dialog({
            height: 100,
            width: 400,
        });
        return false;
    });
    /********* Opens a dialogbox displaying how to set the site as homepage *********/

    /* Auto-open questionnaire dialog
    ****************************************************/
    // Check if popup-cookie is set
    var cookieName = "questionnaireOpened2";
    var cookieValue = $.cookie(cookieName);
    var popupQuestionnaire = $("#popup-questionnaire");
    var endDate = new Date("November 14, 2011");
    var today = new Date();
	//alert(endDate);
    if (endDate > today && cookieValue != "true" && popupQuestionnaire.length != 0) {
        $.cookie(cookieName, "true", { path: '/', domain: 'arkitektnytt.no', secure: false, expires: 21 });
        popupQuestionnaire.dialog({
            modal: true,
            width: 680,
            height: 680,
            close: function (event, ui) { $('#wrap').show(); },
            open: function (event, ui) {
                $('.ui-widget-overlay').bind('click', function () { popupQuestionnaire.dialog('close'); });
                $(this).parents(".ui-dialog").css("background", "#FFFFFF");
                $(this).parents(".ui-dialog:first").find(".ui-widget-header").css("background", "#FFFFFF");
            }
        });
    }
});

